npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-deep-freeze

v0.0.2

Published

[![NPM version](https://badge.fury.io/js/simple-deep-freeze.svg)](http://badge.fury.io/js/simple-deep-freeze) [![peerDependencies Status](https://david-dm.org/remix/simple-deep-freeze/peer-status.svg)](https://david-dm.org/remix/simple-deep-freeze?type=pe

Downloads

689

Readme

simpleDeepFreeze

NPM version peerDependencies Status devDependencies Status Open Source Love Build Status

Recursively Object.freeze simple Javascript structures consisting of plain objects, arrays, and primitives. Make your data immutable.

Use simpleDeepFreeze when setting your application state to make sure you can use reference equality when determining which parts of your app to update, and to make sure it's always serializable.

Example

var simpleDeepFreeze = require('simple-deep-freeze');

var obj = { a: { b: 1}, c: [2, 3] };
simpleDeepFreeze(obj);
obj.a.b = 2; // error (when in 'use strict' mode)
console.log(obj.a.b); // 1
obj.c[0] = 100; // error (when in 'use strict' mode)
console.log(obj.c[0]); // 2

simpleDeepFreeze(Buffer); // error
simpleDeepFreeze(function() {}); // error

In production environments (based on process.env.NODE_ENV) we skip freezing to improve performance.

When encountering an object that is already frozen, we assume it has been frozen recursively already. Make sure that this assumption is true by not using libraries that shallow-freeze objects, and by not shallow-freezing objects yourself. We recommend using the following ESLint rule:

'no-restricted-properties': ['error', {
  object: 'Object',
  property: 'freeze',
  message: 'Use simpleDeepFreeze instead.',
}]

Why?

Freezing data structures helps with certain performance optimisations, such as being able to use reference equality to check if the data structure has not changed, instead of a deep comparison.

Using only plain objects, arrays, and primitives makes it easy to serialize and deserialize data, e.g. using JSON. This is useful to synchronise data between different computers or processes (e.g. persisting data, synchronising between browser windows, and so on).

You could also use simpleDeepFreeze as an intermediate step before using a more full-fledged immutability library like Immutable.js. Or if you don't want to use Immutable.js because of its issues.

Alternatives

  • Immutable.js, as mentioned above.
  • seamless-immutable tries to make sure you're working with immutable data everywhere throughout your application, by overriding methods like Array.prototype.map. This is pretty complicated and error-prone. In contrast, we recommend using simpleDeepFreeze only at critical touchpoints in your application, such as setting your application state. This gives you largely the same benefit, but without the complexity.
  • deep-freeze is very similar but does not enforce simple data structures. People have had a few issues with the library because of this.

License

MIT