
Security News
The Next Open Source Security Race: Triage at Machine Speed
Claude Opus 4.6 has uncovered more than 500 open source vulnerabilities, raising new considerations for disclosure, triage, and patching at scale.
js-object-clone
Advanced tools
Deep cloning and comparison for ES5
// just for convenience
var log = function(){ console.log.apply(console, [].slice.call(arguments)) };
var src = { name: 'dankogai', lang: ['perl'] };
var dst = Object.clone(src); // shallow copy
log( Object.is(src, dst) ); // false
log( Object.equals(src, dst) ); // true
dst.lang.push('javascript');
log(JSON.stringify(src.lang) ); // ["perl","javascript"] because dst is shallow-copied
dst = Object.clone(src, true); // deep copy
dst.lang = dst.lang.reverse();
log( JSON.stringify(src.lang) ); // ["perl","javascript"]
log( JSON.stringify(dst.lang) ); // ["javascript","perl"]
EcmaScript 5 compliance.
This script installs following functions to Object
Clones the object obj. When deep is true, it attempts to deep clone obj.
Unlike many other implementations of object cloners, This one:
Object.getOwnPropertyDescriptor() returnsObject.preventExtensions()Object.seal()Object.freeze()You can clone custom objects so long as its constructor is written in JavaScript:
var Point = function(x, y) {
if (!(this instanceof Point)) return new Point(x, y);
this.x = x*1;
this.y = y*1;
};
Point.prototype = {
distance: function(pt) {
if (!pt) pt = Point(0,0);
var dx = this.x - pt.x;
var dy = this.y - pt.y;
return Math.sqrt(dx*dx + dy*dy);
}
};
var src = Point(3,4);
var dst = Object.clone(src, true);
log( Object.equals(src, dst) ); // false
log( dst.distance(Point(0,0)) ); // 5
If the type of obj is unsupported, it throws TypeError:
dst = Object.clone(new Error); // [object Error] unsupported
Note DOM Elements are not supported. It already has .cloneNode so use it.
cf. https://developer.mozilla.org/en-US/docs/DOM/Node.cloneNode
It is rather trivial to add support for that since all you have to do is delegate it to obj.cloneNode( deep ) (as a matter of fact my early code did support that). But the author decided to drop that since uneval() of Firefox does not support that.
Compares the value of each property in objX and objY and returns true iff all properties are equal, otherwise false.
Like Object.clone(), Object.equals():
Object.is() and Object.isnt()The following ES6 functions are also defined unless predefined (like Chrome 25):
See http://wiki.ecmascript.org/doku.php?id=harmony:egal for details.
Like JSON, Object.clone() and Object.equals() cannot handle circular references.
It is not impossible to handle circular references in JavaScript since you can check if the objects are identical via === operator. Yet it is very impractical without object ID like object_id of Ruby or refaddr of Perl. Without object ID you have to linear search just to check if the object is already visited. As a matter of fact the reference implementation of Map and Set of ES6 resorts to linear search.
With ES5 you can add hidden, immutable properties like .__id__ via Object.defineProperty but mutating objects for that is rude if not unforgivable.
Available only on firefox. Handles circular references.
Lacks deep cloning support and ES5 support. One of the reason why I resorted to writing this.
Lo-dash has _.cloneDeep() yet still lacks ES5 suppport.
Roughly the same but Blob, File and other user-agent specific objects are not yet supported.
FAQs
Deep cloning and comparison for ES5
The npm package js-object-clone receives a total of 53 weekly downloads. As such, js-object-clone popularity was classified as not popular.
We found that js-object-clone demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Claude Opus 4.6 has uncovered more than 500 open source vulnerabilities, raising new considerations for disclosure, triage, and patching at scale.

Research
/Security News
Malicious dYdX client packages were published to npm and PyPI after a maintainer compromise, enabling wallet credential theft and remote code execution.

Security News
gem.coop is testing registry-level dependency cooldowns to limit exposure during the brief window when malicious gems are most likely to spread.