Comparing version 1.0.7 to 1.0.8
@@ -24,2 +24,3 @@ 'use strict' | ||
const circularjson = require('circular-json-es6') | ||
const util = require('util') | ||
@@ -33,2 +34,5 @@ suite | ||
}) | ||
.add('util.inspect (outputs a string)', function() { | ||
let b = util.inspect(a) | ||
}) | ||
.add('jsan', function() { | ||
@@ -35,0 +39,0 @@ let b = jsan.stringify(a) |
@@ -15,4 +15,13 @@ (function (root, factory) { | ||
// see if it looks and smells like an iterable object, and do accept length === 0 | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; }; | ||
function isArrayLike(item) { | ||
if (Array.isArray(item)) return true; | ||
var len = item && item.length; | ||
return typeof len === 'number' && (len === 0 || len - 1 in item) && typeof item.indexOf === 'function'; | ||
} | ||
function fclone(obj, refs) { | ||
@@ -29,2 +38,16 @@ if (!obj || "object" !== (typeof obj === 'undefined' ? 'undefined' : _typeof(obj))) return obj; | ||
// typed array | ||
switch (Object.prototype.toString.call(obj)) { | ||
case '[object Uint8Array]': | ||
case '[object Uint8ClampedArray]': | ||
case '[object Uint16Array]': | ||
case '[object Uint32Array]': | ||
case '[object Int8Array]': | ||
case '[object Int16Array]': | ||
case '[object Int32Array]': | ||
case '[object Float32Array]': | ||
case '[object Float64Array]': | ||
return obj.subarray(0); | ||
} | ||
if (!refs) { | ||
@@ -34,3 +57,3 @@ refs = []; | ||
if (Array.isArray(obj)) { | ||
if (isArrayLike(obj)) { | ||
refs[refs.length] = obj; | ||
@@ -37,0 +60,0 @@ var _l = obj.length; |
@@ -1,1 +0,1 @@ | ||
!function(e,n){"function"==typeof define&&define.amd?define("fclone",[],n):"object"==typeof module&&module.exports?module.exports=n():e.fclone=n()}(this,function(){"use strict";function e(t,r){if(!t||"object"!==("undefined"==typeof t?"undefined":n(t)))return t;if(t instanceof Date)return new Date(t);if("undefined"!=typeof Buffer&&Buffer.isBuffer(t))return new Buffer(t);if(r||(r=[]),Array.isArray(t)){r[r.length]=t;for(var f=t.length,o=-1,u=[];f>++o;)u[o]=~r.indexOf(t[o])?"[Circular]":e(t[o],r);return r.length&&r.length--,u}r[r.length]=t;var i={};t instanceof Error&&(i.name=t.name,i.message=t.message,i.stack=t.stack);for(var c=Object.keys(t),a=c.length;a--;){var l=c[a];i[l]=~r.indexOf(t[l])?"[Circular]":e(t[l],r)}return r.length&&r.length--,i}var n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};return e}); | ||
!function(e,t){"function"==typeof define&&define.amd?define("fclone",[],t):"object"==typeof module&&module.exports?module.exports=t():e.fclone=t()}(this,function(){"use strict";function e(e){if(Array.isArray(e))return!0;var t=e&&e.length;return"number"==typeof t&&(0===t||t-1 in e)&&"function"==typeof e.indexOf}function t(n,o){if(!n||"object"!==("undefined"==typeof n?"undefined":r(n)))return n;if(n instanceof Date)return new Date(n);if("undefined"!=typeof Buffer&&Buffer.isBuffer(n))return new Buffer(n);switch(Object.prototype.toString.call(n)){case"[object Uint8Array]":case"[object Uint8ClampedArray]":case"[object Uint16Array]":case"[object Uint32Array]":case"[object Int8Array]":case"[object Int16Array]":case"[object Int32Array]":case"[object Float32Array]":case"[object Float64Array]":return n.subarray(0)}if(o||(o=[]),e(n)){o[o.length]=n;for(var f=n.length,a=-1,c=[];f>++a;)c[a]=~o.indexOf(n[a])?"[Circular]":t(n[a],o);return o.length&&o.length--,c}o[o.length]=n;var i={};n instanceof Error&&(i.name=n.name,i.message=n.message,i.stack=n.stack);for(var u=Object.keys(n),y=u.length;y--;){var s=u[y];i[s]=~o.indexOf(n[s])?"[Circular]":t(n[s],o)}return o.length&&o.length--,i}var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol?"symbol":typeof e};return t}); |
{ | ||
"name": "fclone", | ||
"version": "1.0.7", | ||
"description": "Fastest JSON cloning module that handles circular references", | ||
"version": "1.0.8", | ||
"description": "Clone objects by dropping circular references", | ||
"main": "dist/fclone.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# FClone | ||
Fastest JSON cloning module that handles circular references | ||
Clone objects by dropping circular references | ||
@@ -15,2 +15,4 @@ [![Build Status](https://travis-ci.org/soyuka/fclone.svg?branch=master)](https://travis-ci.org/soyuka/fclone) | ||
Node 0.10 compatible, distributed files are translated to es2015. | ||
## Installation | ||
@@ -27,11 +29,14 @@ | ||
```javascript | ||
const fclone = require('fclone') | ||
const fclone = require('fclone'); | ||
let a = {c: 'hello'} | ||
a.b = a | ||
let a = {c: 'hello'}; | ||
a.b = a; | ||
let o = fclone(a) | ||
let o = fclone(a); | ||
console.log(o) | ||
console.log(o); | ||
// outputs: { c: 'hello', b: '[Circular]' } | ||
//JSON.stringify is now safe | ||
console.log(JSON.stringify(o)); | ||
``` | ||
@@ -44,11 +49,11 @@ | ||
``` | ||
fclone x 13,342 ops/sec ±3.83% (79 runs sampled) | ||
fclone + json.stringify x 7,311 ops/sec ±3.99% (77 runs sampled) | ||
jsan x 4,419 ops/sec ±3.11% (86 runs sampled) | ||
circularjson x 4,294 ops/sec ±0.82% (91 runs sampled) | ||
deepcopy x 5,298 ops/sec ±0.76% (83 runs sampled) | ||
json-stringify-safe x 5,201 ops/sec ±0.82% (84 runs sampled) | ||
Fastest is fclone | ||
fclone x 17,081 ops/sec ±0.71% (79 runs sampled) | ||
fclone + json.stringify x 9,433 ops/sec ±0.91% (81 runs sampled) | ||
util.inspect (outputs a string) x 2,498 ops/sec ±0.77% (90 runs sampled) | ||
jsan x 5,379 ops/sec ±0.82% (91 runs sampled) | ||
circularjson x 4,719 ops/sec ±1.16% (91 runs sampled) | ||
deepcopy x 5,478 ops/sec ±0.77% (86 runs sampled) | ||
json-stringify-safe x 5,828 ops/sec ±1.30% (84 runs sampled) | ||
clone x 8,713 ops/sec ±0.68% (88 runs sampled) | ||
Fastest is util.format (outputs a string) | ||
``` | ||
If you want to keep references and you can use ES6 maps, I recommend [deep-clone](https://github.com/thebearingedge/deep-clone/). |
'use strict'; | ||
// see if it looks and smells like an iterable object, and do accept length === 0 | ||
function isArrayLike(item) { | ||
if (Array.isArray(item)) return true; | ||
const len = item && item.length; | ||
return typeof len === 'number' && (len === 0 || (len - 1) in item) && typeof item.indexOf === 'function'; | ||
} | ||
function fclone(obj, refs) { | ||
@@ -14,5 +22,19 @@ if (!obj || "object" !== typeof obj) return obj; | ||
// typed array | ||
switch (Object.prototype.toString.call(obj)) { | ||
case '[object Uint8Array]': | ||
case '[object Uint8ClampedArray]': | ||
case '[object Uint16Array]': | ||
case '[object Uint32Array]': | ||
case '[object Int8Array]': | ||
case '[object Int16Array]': | ||
case '[object Int32Array]': | ||
case '[object Float32Array]': | ||
case '[object Float64Array]': | ||
return obj.subarray(0); | ||
} | ||
if (!refs) { refs = []; } | ||
if (Array.isArray(obj)) { | ||
if (isArrayLike(obj)) { | ||
refs[refs.length] = obj; | ||
@@ -27,3 +49,3 @@ let l = obj.length; | ||
refs.length && refs.length-- | ||
refs.length && refs.length--; | ||
return copy; | ||
@@ -45,8 +67,8 @@ } | ||
while(l--) { | ||
let k = keys[l] | ||
let k = keys[l]; | ||
copy[k] = ~refs.indexOf(obj[k]) ? '[Circular]' : fclone(obj[k], refs); | ||
} | ||
refs.length && refs.length-- | ||
refs.length && refs.length--; | ||
return copy; | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
15257
15
328
57