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):
Via CLI:
babel --plugins proto-to-create script.js
Via Node API:
;
Examples
// Inconst foo = __proto__: null bar: 2 { thisbar++ }
// Outvar foo = _ref = Object _refbar = 2 _ref { thisbar++} _ref;
// Inconst foo = foo: 1 { thisfoo++ } const bar = __proto__: foo bar: 2 { thisbar++ }
// Outvar _ref; var foo = foo: 1 { thisfoo++ }; var bar = _ref = Object _refbar = 2 _ref { thisbar++} _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 = { /* ... */ } const Mixin2 = { /* ... */ } const Foo = { /* ... */ } { /* ... */ } const Type = __proto__: Foo ...Mixin1 ...Mixin2 // Override { /* ... */ } const inst = Object // 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:
- Strings are always double-quoted when not interpolating.
- Function names and their opening parenthesis have no space before them, i.e.
function foo() {}
- Always include the trailing comma.
- Never use
var
. - 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.