readme.md (1196B)
1 # define-lazy-prop 2 3 > Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object 4 5 Useful when the value of a property is expensive to generate, so you want to delay the computation until the property is needed. For example, improving startup performance by deferring nonessential operations. 6 7 ## Install 8 9 ``` 10 $ npm install define-lazy-prop 11 ``` 12 13 ## Usage 14 15 ```js 16 import defineLazyProperty from 'define-lazy-prop'; 17 18 const unicorn = { 19 // … 20 }; 21 22 defineLazyProperty(unicorn, 'rainbow', () => expensiveComputation()); 23 24 app.on('user-action', () => { 25 doSomething(unicorn.rainbow); 26 }); 27 ``` 28 29 ## API 30 31 ### defineLazyProperty(object, propertyName, valueGetter) 32 33 #### object 34 35 Type: `object` 36 37 Object to add the property to. 38 39 #### propertyName 40 41 Type: `string` 42 43 Name of the property to add. 44 45 #### valueGetter 46 47 Type: `Function` 48 49 Called the first time `propertyName` is accessed. Expected to return a value. 50 51 ## Related 52 53 - [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value 54 - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily 55 - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise