Skip to content
ES6 Proxies based deep clone
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
support Fix 'use strict' directive Jan 23, 2018
.gitignore bump all the dependencies Jun 17, 2019
.travis.yml travis: update node versions, sudo false Jun 17, 2019
History.md Release 0.1.1 May 23, 2014
Readme.md publish to npm again Mar 23, 2018
index.js throw on (yet) unimplemented handlers Jan 23, 2018
package-lock.json bump some dev deps Jul 11, 2019
package.json
test.js modernize! Jan 23, 2018

Readme.md

proxy-clone build status

ES6 Proxies based deep clone, that's way more efficient than the traditional clone implementations when dealing with big objects.

Requires node 6 or later.

Note: This is not a traditional clone(). Changes to the source object will be reflected in the clone, changes to the clone however won't modify the source.

Performance

Depending on the object size, proxy-clone can be a lot faster than naive JSON clone or the clone module from npm. The most important thing to note is that clone speed is constant, however using the cloned object is slightly slower.

JSON small x 60,036 ops/sec ±1.09% (92 runs sampled)
JSON medium x 5,919 ops/sec ±0.86% (91 runs sampled)
JSON big x 526 ops/sec ±1.20% (89 runs sampled)
JSON gigantic x 39.50 ops/sec ±1.83% (54 runs sampled)

clone small x 50,288 ops/sec ±1.20% (91 runs sampled)
clone medium x 4,381 ops/sec ±1.03% (90 runs sampled)
clone big x 230 ops/sec ±0.85% (85 runs sampled)
clone gigantic x 4.14 ops/sec ±1.54% (15 runs sampled)

proxy-clone small x 842,147 ops/sec ±1.18% (93 runs sampled)
proxy-clone medium x 891,579 ops/sec ±1.49% (87 runs sampled)
proxy-clone big x 814,796 ops/sec ±0.83% (92 runs sampled)
proxy-clone gigantic x 792,461 ops/sec ±0.79% (89 runs sampled)

Installation

$ npm install proxy-clone

Stability

This module makes certain assumptions about what you do with the cloned object, and I only tested it with the operations one project required. If something behaves odly, open an issue and I'll look into it.

Example

The api is what you'd expect:

var clone = require('proxy-clone');
var assert = require('assert');

var obj = {
  foo: {
    bar: 'baz'
  }
};

var cloned = clone(obj);
assert.deepEqual(cloned, obj);

API

clone(obj)

Return a deep clone of obj.

How it works

Traditional clone implementations recursively iterate over the object and copy all the properties to a new object, which can be slow. This module instead creates an ES6 Proxy. When you change a property on the proxy, it adds it to an internal change log. When you read from the proxy, it first checks for overrides, otherwise returns the original value from the object.

Kudos

Thanks to @segmentio for letting me publish this private module that I developed while working for them.

License

MIT

You can’t perform that action at this time.