Comparing version 3.1.4 to 3.1.5
var pathGetter = require('./path-getter'); | ||
var utils = require('./utils'); | ||
var map = typeof WeakMap !== 'undefined'? | ||
new WeakMap(): | ||
var Map = typeof WeakMap !== 'undefined'? | ||
WeakMap: | ||
function() { | ||
@@ -22,3 +22,3 @@ var keys = []; | ||
} | ||
}(); | ||
}; | ||
@@ -30,2 +30,6 @@ // Based on https://github.com/douglascrockford/JSON-js/blob/master/cycle.js | ||
var map = new Map() | ||
var hasCircular = Object.prototype.hasOwnProperty.call(options, 'circular'); | ||
return (function derez(_value, path, key) { | ||
@@ -35,3 +39,3 @@ | ||
var i, // The loop counter | ||
var i, // The loop counter | ||
name, // Property name | ||
@@ -84,2 +88,7 @@ nu; // The new object or array | ||
if (foundPath) { | ||
if (hasCircular && path.indexOf(foundPath) === 0) { | ||
return typeof options.circular === 'function'? | ||
options.circular(value, path, foundPath): | ||
options.circular; | ||
} | ||
return {$jsan: foundPath}; | ||
@@ -86,0 +95,0 @@ } |
{ | ||
"name": "jsan", | ||
"version": "3.1.4", | ||
"version": "3.1.5", | ||
"description": "handle circular references when stringifying and parsing", | ||
"main": "index.js", | ||
"scripts": { | ||
"benchmark": "rimraf 'perf/*.txt' && for i in `seq 1 9`; do node perf/bench > perf/$i.txt; done", | ||
"benchmark": "node perf/bench > perf/results.txt", | ||
"test": "mocha" | ||
@@ -16,2 +16,3 @@ }, | ||
"devDependencies": { | ||
"benchmark": "^2.1.2", | ||
"circular-json": "^0.3.0", | ||
@@ -18,0 +19,0 @@ "immutable": "^3.7.6", |
@@ -7,45 +7,52 @@ var jsan = require('../'); | ||
var Benchmark = require('benchmark'); | ||
var decycledGlobal = decycle(global, {}); | ||
function suite(name, obj, iterations) { | ||
console.log(name + ' at ' + iterations + ' iterations'); | ||
test('jsan', iterations, jsan.stringify, obj); | ||
test('CircularJSON', iterations, CircularJSON.stringify, obj); | ||
test('json-stringify-safe', iterations, stringify, obj); | ||
console.log(); | ||
const suite = (name, obj) => () => { | ||
return new Promise(function(resolve) { | ||
var hzs = [] | ||
console.log(name) | ||
new Benchmark.Suite(name) | ||
.add('jsan', () => jsan.stringify(obj)) | ||
.add('CircularJSON', () => CircularJSON.stringify(obj)) | ||
.add('json-stringify-safe', () => stringify(obj)) | ||
.on('cycle', event => { | ||
hzs.push(event.target.hz) | ||
console.log(String(event.target)) | ||
}) | ||
.on('complete', function() { | ||
var fastest = this.filter('fastest')[0]; | ||
hzs = hzs.sort().reverse(); | ||
console.log(fastest.name, 'is', ((hzs[0] / hzs[1]) * 100).toFixed(2) + '% faster then the 2nd best'); | ||
console.log(fastest.name, 'is', ((hzs[0] / hzs[2]) * 100).toFixed(2) + '% faster then the 3rd best'); | ||
console.log() | ||
resolve(); | ||
}) | ||
.run({ 'async': true }) | ||
; | ||
}); | ||
}; | ||
function test(name, iterations, fn, obj) { | ||
var ms = time(function() { | ||
for (var i = 0; i < iterations; i++) { | ||
fn(obj); | ||
} | ||
}); | ||
console.log(' ' + name + ' took ' + ms); | ||
} | ||
function time(fn) { | ||
var start = new Date(); | ||
fn(); | ||
return new Date() - start; | ||
} | ||
suite('global', global, 1000); | ||
suite('decycledGlobal', decycledGlobal, 1000); | ||
suite('empty object', {}, 1000000); | ||
suite('empty array', [], 1000000); | ||
suite('small object', {x: 1, y: 2, z: 3}, 100000); | ||
var obj = {x: 1, y: 2, z: 3}; | ||
obj.self = obj; | ||
suite('self referencing small object', {x: 1, y: 2, z: 3}, 100000); | ||
suite('small array', ['x', 'y', 123, 'z'], 100000); | ||
var arr = ['x', 'y', 123, 'z']; | ||
arr.push(arr); | ||
suite('self referencing small array', ['x', 'y', 123, 'z'], 100000); | ||
suite('string', 'this" is \' a test\t\n', 1000000); | ||
suite('number', 1234, 1000000); | ||
suite('null', null, 1000000); | ||
Promise.resolve() | ||
.then(suite('global', global)) | ||
.then(suite('decycledGlobal', decycledGlobal)) | ||
.then(suite('empty object', {})) | ||
.then(suite('empty array', [])) | ||
.then(suite('small object', {x: 1, y: 2, z: 3})) | ||
.then(suite('self referencing small object', obj)) | ||
.then(suite('small array', ['x', 'y', 123, 'z'])) | ||
.then(suite('self referencing small array', arr)) | ||
.then(suite('string', 'this" is \' a test\t\n')) | ||
.then(suite('number', 1234)) | ||
.then(suite('null', null)) |
@@ -80,3 +80,3 @@ var assert = require('assert'); | ||
assert.deepEqual(obj2.f.toString(), obj1.f.toString().toUpperCase()); | ||
}) | ||
}); | ||
@@ -83,0 +83,0 @@ it("doesn't blow up for object with $jsan keys", function() { |
@@ -77,2 +77,12 @@ var assert = require('assert'); | ||
it('can use the circular option', function() { | ||
var obj = {}; | ||
obj.self = obj; | ||
obj.a = 1; | ||
obj.b = {}; | ||
obj.c = obj.b; | ||
assert.equal(jsan.stringify(obj, null, null, {circular: '∞'}), '{"self":"∞","a":1,"b":{},"c":{"$jsan":"$.b"}}'); | ||
assert.equal(jsan.stringify(obj, null, null, {circular: function() { return '∞!' }}), '{"self":"∞!","a":1,"b":{},"c":{"$jsan":"$.b"}}'); | ||
}); | ||
it('works on objects with "[", "\'", and "]" in the keys', function() { | ||
@@ -98,2 +108,11 @@ var obj = {}; | ||
it('works correctly for mutiple calls with the same object', function() { | ||
var obj = {}; | ||
obj.self = obj; | ||
obj.a = {}; | ||
obj.b = obj.a; | ||
assert.equal(jsan.stringify(obj), '{"self":{"$jsan":"$"},"a":{},"b":{"$jsan":"$.a"}}'); | ||
assert.equal(jsan.stringify(obj), '{"self":{"$jsan":"$"},"a":{},"b":{"$jsan":"$.a"}}'); | ||
}); | ||
}); | ||
@@ -100,0 +119,0 @@ |
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
606
29095
7
17