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

babel-plugin-proto-to-create

v0.3.0

Published

Convert `__proto__` in object literals to `Object.create` + property assign

Downloads

3

Readme

babel-plugin-proto-to-create

A simple plugin that converts __proto__ in object literals to Object.create + setting properties. Engines haven't historically optimized literals with __proto__ very well. Some just don't optimize object accesses as well (SpiderMonkey), and others refuse to optimize the entire containing function context (V8). That's where this plugin comes in. See this blog post for why I made this.

Usage

Via .babelrc (recommended):

{
    "plugins": ["proto-to-create"]
}

Via CLI:

babel --plugins proto-to-create script.js

Via Node API:

require("babel-core").transform("code", {
    plugins: ["proto-to-create"],
});

Examples

// In
const foo = {
    __proto__: null,
    bar: 2,
    doSomething() {
        this.bar++
    },
}
// Out
var foo = (_ref = Object.create(null), _ref.bar = 2, _ref.doSomething = function () {
    this.bar++
}, _ref);
// In
const foo = {
    foo: 1,
    doSomething() {
        this.foo++
    },
}

const bar = {
    __proto__: foo,
    bar: 2,
    doSomethingElse() {
        this.bar++
    },
}
// Out
var _ref;

var foo = {
    foo: 1,
    doSomething: function () {
        this.foo++
    },
};

var bar = (_ref = Object.create(foo), _ref.bar = 2, _ref.doSomethingElse = function () {
    this.bar++
}, _ref);

This works well with @JedWatson's babel-plugin-object-assign plugin. It's also designed to make prototypal inheritance with objects linked to other object faster and better. Also it works with object spread properties when the es7.objectRestSpread transformer is enabled, making for some very sweet, easy object prototype mixins that still don't lag in performance.

const Mixin1 = {
    someCoolAddition() { /* ... */ },
}

const Mixin2 = {
    anotherCoolMethod() { /* ... */ },
}

const Foo = {
    master() { /* ... */ },

    method() { /* ... */ },
}

const Type = {
    __proto__: Foo,

    ...Mixin1,
    ...Mixin2,

    // Override
    method() { /* ... */ },
}

const inst = Object.create(Type)

// Or, you could use __proto__ as an initializer, even, with little of a
// performance hit.

const other = {
    __proto__: Type,

    // An extra property
    prop: 1,
}

Caveats

This does nothing about setting __proto__ directly. There is no decent way to mitigate the performance impact of dynamically setting object prototypes directly, and trying to optimize this use case is out of the scope of this library.

foo.__proto__ = bar

Issues

If you run into problems, please file an issue in the issue tracker. I will take a look at it as soon as I get to it.

Contributing

Feel free to open a PR if you think something could be done better, or another thing awesome could be added. Make sure to keep ESLint happy, and keep this as well tested as possible. When you write tests, please follow the directions in the test readme.

Code style

This follows the standard code style, with a few modifications:

  1. Strings are always double-quoted when not interpolating.
  2. Function names and their opening parenthesis have no space before them, i.e. function foo() {}
  3. Always include the trailing comma.
  4. Never use var.
  5. Indentation is 4 spaces.

License

ISC License

Copyright (c) 2015, Isiah Meadows

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.