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

metaproperties

v1.1.2

Published

Store information about objects without modifying them.

Downloads

3

Readme

⏫ metaproperties

Associate data with any object without modifying the object in any way. Implemented using WeakMaps. Works in browser and node.js.

const meta = require('metaproperties');

meta(someObject).someProperty = {
  created: 'yesterday',
  foo: 'bar'
};

someObject.someProperty // undefined

meta(someObject).someProperty.foo // 'bar'

meta(someObject) = 0;
meta(someObject)++;
// etc

motivation

There are many cases where one would like to store some information about or on an object, without affecting its state. For example, on objects created by an external library. Aside from defining a new property on the object, one would have to use a mapping implementation to associate the object with desired data.

Prior to the introduction of Maps/WeakMaps, objects couldn't be used as keys in hash-map-like implementations. Typically, and object would be assigned a unique string identifier as a property, which would be used as the key of a map object. However, the new data structures are designed solely to function as hash-maps, and they allow objects to be used as keys directly, which improves performance.

metaproperties is a thin abstraction over a WeakMap, offering some convenience and syntactic simplicity.

installation

npm install metaproperties --save

syntax

require('metaproperties')(object: Object [, key: Symbol]): Object
require('metaproperties').createKey(): Symbol

usage

const varsof = require('metaproperties');

let object = {
  weight: 100,
  shape: 'round',
  color: '#00ff00',
  child: {
    born: false
  }
};

varsof(object).secretProperty = 'foo';

console.log(object.secretProperty); // undefined, as expected

console.log(varsof(object).secretProperty); // foo, as expected

objects unaffected

let beforeProps = Object.getOwnPropertyNames(object), // properties on object
    beforeSyms = Object.getOwnPropertySymbols(object); // symbols on object
    
varsof(object).secretProperty = 'foo';

let afterProps = Object.getOwnPropertyNames(object),
    afterSyms = Object.getOwnPropertySymbols(object);
    
console.log(beforeProps.length === afterProps.length && beforeSyms.length ===
afterSyms.length); // true, as expected

let same = true;
beforeProps.forEach((prop, i) => {
  same = same && beforeProps[i] === afterProps[i]
});
beforeSyms.forEach((sym, i) => {
  same = same && beforeSyms[i] === afterSyms[i]
});

console.log(same); // true, as expected

performance

Without using the secret key functionality described below, performance is just slightly slower than native.

Run benchmark.js; this does not test secret key functionality. The first line represents native (setting properties directly on object) performance, and the second line represents metaproperties' performance. My typical results in node.js:

31.37ms average (native)
32.31ms average (meta)

Performance penalty should be minimal if varsof(...) is not used in critical performance sections.

Please let me know if your results differ!

Using the secret key functionality results in twice as slow as native performance; however, performance issues should generally not be noticeable.

secret keys

Specify a key to use as an access key. Must use varsof.createKey to create a key.

const varsof = require('metaproperties');

let key = varsof.createKey();

varsof(someObject, key).bigSecret = 'abc123';

varsof(someObject).bigSecret // undefined, as expected

varsof(someObject, key).bigSecret // 'abc123';

Dispose of access keys once no longer needed:

varsof.destroyKey(key: Symbol): void

etymology

"metaproperties" sounded cool. Also, metadata + properties = metaproperties.

license

MIT

feedback

Create issues here on github or email metapropertiesfeedback [at symbol] [google's email service].