# define-lazy-prop

> Define a lazily evaluated property on an object

Latest version **3.0.0** (published 2021-04-14) · MIT license · 0 weekly downloads

## Install

```sh
npm install define-lazy-prop
pnpm add define-lazy-prop
yarn add define-lazy-prop
bun add define-lazy-prop
```

## Health

**Score 28/100 (F)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.0 |
| Published | 2021-04-14 |
| First published | 2017-01-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/define-lazy-prop) |
| Module format | ESM |
| Node | >=12 |
| Dependencies | 0 |
| Unpacked size | 4.3 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 67 |
| Author | Sindre Sorhus |
| Maintainers | sindresorhus |
| Keywords | lazy, property, properties, prop, define, object, value, lazily, laziness, evaluation, eval, execute, getter, function, fn, memoize, cache, defer, deferred |

## Links

- npm: https://www.npmjs.com/package/define-lazy-prop
- Repository: https://github.com/sindresorhus/define-lazy-prop
- Homepage: https://github.com/sindresorhus/define-lazy-prop#readme
- Issues: https://github.com/sindresorhus/define-lazy-prop/issues
- Funding: https://github.com/sponsors/sindresorhus
- npm.io page: https://npm.io/package/define-lazy-prop

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 3.0.0 (latest) — 2021-04-14
- 2.0.0 — 2019-04-10
- 1.0.0 — 2017-01-15

## README

# define-lazy-prop

> Define a [lazily evaluated](https://en.wikipedia.org/wiki/Lazy_evaluation) property on an object

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.

## Install

```
$ npm install define-lazy-prop
```

## Usage

```js
import defineLazyProperty from 'define-lazy-prop';

const unicorn = {
	// …
};

defineLazyProperty(unicorn, 'rainbow', () => expensiveComputation());

app.on('user-action', () => {
	doSomething(unicorn.rainbow);
});
```

## API

### defineLazyProperty(object, propertyName, valueGetter)

#### object

Type: `object`

Object to add the property to.

#### propertyName

Type: `string`

Name of the property to add.

#### valueGetter

Type: `Function`

Called the first time `propertyName` is accessed. Expected to return a value.

## Related

- [lazy-value](https://github.com/sindresorhus/lazy-value) - Create a lazily evaluated value
- [import-lazy](https://github.com/sindresorhus/import-lazy) - Import a module lazily
- [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise

---
_Source: https://npm.io/package/define-lazy-prop · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
