Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsan

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsan - npm Package Compare versions

Comparing version 3.1.3 to 3.1.4

47

lib/cycle.js
var pathGetter = require('./path-getter');
var utils = require('./utils');
var map = typeof WeakMap !== 'undefined'?
new WeakMap():
function() {
var keys = [];
var values = [];
return {
set: function(key, value) {
keys.push(key);
values.push(value);
},
get: function(key) {
for (var i = 0; i < keys.length; i++) {
if (keys[i] === key) {
return values[i];
}
}
}
}
}();
// Based on https://github.com/douglascrockford/JSON-js/blob/master/cycle.js

@@ -9,5 +29,2 @@

var objects = [], // Keep a reference to each unique object or array
paths = []; // Keep the path to each unique object or array
return (function derez(_value, path, key) {

@@ -41,5 +58,9 @@

}
if (options['symbol'] && typeof value === 'symbol') {
// 'Symbol(foo)'.slice(7, -1) === 'foo'
return {$jsan: 's' + value.toString().slice(7, -1)}
}
if (value && typeof value.toJSON === 'function') {
value = value.toJSON();
value = value.toJSON(key);
}

@@ -53,19 +74,17 @@

!(value instanceof String) &&
!(typeof value === 'symbol') &&
!(value instanceof Error)) {
// If the value is an object or array, look to see if we have already
// encountered it. If so, return a $ref/path object. This is a hard way,
// linear search that will get slower as the number of unique objects grows.
// encountered it. If so, return a $ref/path object.
for (i = 0; i < objects.length; i += 1) {
if (objects[i] === value) {
return {$jsan: paths[i]};
}
if (typeof value === 'object' && value !== null) {
var foundPath = map.get(value);
if (foundPath) {
return {$jsan: foundPath};
}
map.set(value, path);
}
// Otherwise, accumulate the unique value and its path.
objects.push(value);
paths.push(path);
// If it is an array, replicate the array.

@@ -72,0 +91,0 @@

@@ -22,3 +22,4 @@ var cycle = require('./cycle');

'undefined': options,
'error': options
'error': options,
'symbol': options
}

@@ -25,0 +26,0 @@ }

@@ -45,2 +45,4 @@ var pathGetter = require('./path-getter');

return error;
case 's':
return Symbol(rest);
default:

@@ -47,0 +49,0 @@ console.warn('unknown type', obj);

{
"name": "jsan",
"version": "3.1.3",
"version": "3.1.4",
"description": "handle circular references when stringifying and parsing",

@@ -5,0 +5,0 @@ "main": "index.js",

global at 1000 iterations
jsan took 1292
CircularJSON took 1093
json-stringify-safe took 1770
jsan took 21
CircularJSON took 800
json-stringify-safe took 746
decycledGlobal at 1000 iterations
jsan took 147
CircularJSON took 537
json-stringify-safe took 1188
jsan took 134
CircularJSON took 605
json-stringify-safe took 556
empty object at 1000000 iterations
jsan took 369
CircularJSON took 512
json-stringify-safe took 698
jsan took 347
CircularJSON took 787
json-stringify-safe took 1016
empty array at 1000000 iterations
jsan took 369
CircularJSON took 348
json-stringify-safe took 486
jsan took 411
CircularJSON took 483
json-stringify-safe took 627
small object at 100000 iterations
jsan took 49
CircularJSON took 170
json-stringify-safe took 531
CircularJSON took 243
json-stringify-safe took 326
self referencing small object at 100000 iterations
jsan took 50
CircularJSON took 164
json-stringify-safe took 543
jsan took 48
CircularJSON took 243
json-stringify-safe took 331
small array at 100000 iterations
jsan took 55
CircularJSON took 134
json-stringify-safe took 561
jsan took 47
CircularJSON took 248
json-stringify-safe took 290
self referencing small array at 100000 iterations
jsan took 43
CircularJSON took 126
json-stringify-safe took 544
jsan took 46
CircularJSON took 292
json-stringify-safe took 282
string at 1000000 iterations
jsan took 295
CircularJSON took 306
json-stringify-safe took 435
jsan took 283
CircularJSON took 344
json-stringify-safe took 485
number at 1000000 iterations
jsan took 227
CircularJSON took 210
json-stringify-safe took 353
jsan took 243
CircularJSON took 235
json-stringify-safe took 385
null at 1000000 iterations
jsan took 222
CircularJSON took 204
json-stringify-safe took 362
jsan took 224
CircularJSON took 249
json-stringify-safe took 389
global at 1000 iterations
jsan took 1351
CircularJSON took 1128
json-stringify-safe took 1715
jsan took 21
CircularJSON took 812
json-stringify-safe took 774
decycledGlobal at 1000 iterations
jsan took 127
CircularJSON took 546
json-stringify-safe took 1142
jsan took 138
CircularJSON took 643
json-stringify-safe took 634
empty object at 1000000 iterations
jsan took 366
CircularJSON took 504
json-stringify-safe took 658
jsan took 406
CircularJSON took 833
json-stringify-safe took 948
empty array at 1000000 iterations
jsan took 368
CircularJSON took 323
json-stringify-safe took 539
jsan took 349
CircularJSON took 440
json-stringify-safe took 586
small object at 100000 iterations
jsan took 46
CircularJSON took 162
json-stringify-safe took 505
jsan took 49
CircularJSON took 231
json-stringify-safe took 343
self referencing small object at 100000 iterations
jsan took 56
CircularJSON took 178
json-stringify-safe took 499
jsan took 47
CircularJSON took 230
json-stringify-safe took 335
small array at 100000 iterations
jsan took 44
CircularJSON took 140
json-stringify-safe took 555
jsan took 53
CircularJSON took 282
json-stringify-safe took 319
self referencing small array at 100000 iterations
jsan took 50
CircularJSON took 143
json-stringify-safe took 523
CircularJSON took 294
json-stringify-safe took 302
string at 1000000 iterations
jsan took 292
CircularJSON took 321
json-stringify-safe took 459
jsan took 285
CircularJSON took 347
json-stringify-safe took 494
number at 1000000 iterations
jsan took 233
CircularJSON took 201
json-stringify-safe took 337
jsan took 239
CircularJSON took 251
json-stringify-safe took 386
null at 1000000 iterations
jsan took 211
CircularJSON took 206
json-stringify-safe took 349
jsan took 222
CircularJSON took 257
json-stringify-safe took 434
global at 1000 iterations
jsan took 1345
CircularJSON took 1134
json-stringify-safe took 1774
jsan took 27
CircularJSON took 832
json-stringify-safe took 856
decycledGlobal at 1000 iterations
jsan took 136
CircularJSON took 531
json-stringify-safe took 1173
jsan took 139
CircularJSON took 680
json-stringify-safe took 595
empty object at 1000000 iterations
jsan took 352
CircularJSON took 513
json-stringify-safe took 690
jsan took 437
CircularJSON took 855
json-stringify-safe took 1052
empty array at 1000000 iterations
jsan took 352
CircularJSON took 347
json-stringify-safe took 471
jsan took 421
CircularJSON took 437
json-stringify-safe took 591
small object at 100000 iterations
jsan took 50
CircularJSON took 166
json-stringify-safe took 523
CircularJSON took 257
json-stringify-safe took 384
self referencing small object at 100000 iterations
jsan took 49
CircularJSON took 152
json-stringify-safe took 506
jsan took 60
CircularJSON took 252
json-stringify-safe took 388
small array at 100000 iterations
jsan took 48
CircularJSON took 126
json-stringify-safe took 564
jsan took 57
CircularJSON took 277
json-stringify-safe took 335
self referencing small array at 100000 iterations
jsan took 46
CircularJSON took 147
json-stringify-safe took 557
jsan took 54
CircularJSON took 253
json-stringify-safe took 292
string at 1000000 iterations
jsan took 297
CircularJSON took 308
json-stringify-safe took 448
jsan took 279
CircularJSON took 340
json-stringify-safe took 541
number at 1000000 iterations
jsan took 234
CircularJSON took 206
json-stringify-safe took 347
jsan took 252
CircularJSON took 283
json-stringify-safe took 426
null at 1000000 iterations
jsan took 218
CircularJSON took 207
json-stringify-safe took 353
jsan took 234
CircularJSON took 253
json-stringify-safe took 405
global at 1000 iterations
jsan took 1336
CircularJSON took 1102
json-stringify-safe took 1810
jsan took 21
CircularJSON took 858
json-stringify-safe took 843
decycledGlobal at 1000 iterations
jsan took 121
CircularJSON took 534
json-stringify-safe took 1159
jsan took 139
CircularJSON took 602
json-stringify-safe took 564
empty object at 1000000 iterations
jsan took 359
CircularJSON took 510
json-stringify-safe took 686
jsan took 376
CircularJSON took 886
json-stringify-safe took 1035
empty array at 1000000 iterations
jsan took 369
CircularJSON took 349
json-stringify-safe took 529
jsan took 386
CircularJSON took 501
json-stringify-safe took 706
small object at 100000 iterations
jsan took 55
CircularJSON took 168
json-stringify-safe took 524
jsan took 54
CircularJSON took 223
json-stringify-safe took 321
self referencing small object at 100000 iterations
jsan took 55
CircularJSON took 179
json-stringify-safe took 533
jsan took 58
CircularJSON took 223
json-stringify-safe took 325
small array at 100000 iterations
jsan took 43
CircularJSON took 135
json-stringify-safe took 549
jsan took 45
CircularJSON took 284
json-stringify-safe took 306
self referencing small array at 100000 iterations
jsan took 48
CircularJSON took 138
json-stringify-safe took 527
jsan took 54
CircularJSON took 283
json-stringify-safe took 307
string at 1000000 iterations
jsan took 270
CircularJSON took 298
json-stringify-safe took 424
jsan took 302
CircularJSON took 375
json-stringify-safe took 521
number at 1000000 iterations
jsan took 225
CircularJSON took 198
json-stringify-safe took 334
jsan took 257
CircularJSON took 260
json-stringify-safe took 419
null at 1000000 iterations
jsan took 219
CircularJSON took 201
json-stringify-safe took 332
jsan took 232
CircularJSON took 246
json-stringify-safe took 393
global at 1000 iterations
jsan took 1344
CircularJSON took 1103
json-stringify-safe took 1743
jsan took 20
CircularJSON took 812
json-stringify-safe took 761
decycledGlobal at 1000 iterations
jsan took 126
CircularJSON took 516
json-stringify-safe took 1092
jsan took 134
CircularJSON took 626
json-stringify-safe took 574
empty object at 1000000 iterations
jsan took 364
CircularJSON took 530
json-stringify-safe took 699
jsan took 406
CircularJSON took 764
json-stringify-safe took 990
empty array at 1000000 iterations
jsan took 369
CircularJSON took 320
json-stringify-safe took 489
jsan took 407
CircularJSON took 468
json-stringify-safe took 601
small object at 100000 iterations
jsan took 47
CircularJSON took 158
json-stringify-safe took 498
jsan took 58
CircularJSON took 227
json-stringify-safe took 338
self referencing small object at 100000 iterations
jsan took 56
CircularJSON took 165
json-stringify-safe took 512
jsan took 50
CircularJSON took 231
json-stringify-safe took 373
small array at 100000 iterations
jsan took 48
CircularJSON took 136
json-stringify-safe took 517
jsan took 55
CircularJSON took 314
json-stringify-safe took 317
self referencing small array at 100000 iterations
jsan took 44
CircularJSON took 132
json-stringify-safe took 507
jsan took 52
CircularJSON took 262
json-stringify-safe took 330
string at 1000000 iterations
jsan took 286
CircularJSON took 300
json-stringify-safe took 445
jsan took 331
CircularJSON took 369
json-stringify-safe took 570
number at 1000000 iterations
jsan took 222
CircularJSON took 196
json-stringify-safe took 341
jsan took 242
CircularJSON took 260
json-stringify-safe took 435
null at 1000000 iterations
jsan took 209
CircularJSON took 197
json-stringify-safe took 339
jsan took 236
CircularJSON took 250
json-stringify-safe took 404
global at 1000 iterations
jsan took 1327
CircularJSON took 1072
json-stringify-safe took 1748
jsan took 23
CircularJSON took 838
json-stringify-safe took 848
decycledGlobal at 1000 iterations
jsan took 120
CircularJSON took 521
json-stringify-safe took 1139
jsan took 155
CircularJSON took 683
json-stringify-safe took 615
empty object at 1000000 iterations
jsan took 358
CircularJSON took 490
json-stringify-safe took 652
jsan took 402
CircularJSON took 922
json-stringify-safe took 1016
empty array at 1000000 iterations
jsan took 356
CircularJSON took 334
json-stringify-safe took 480
jsan took 392
CircularJSON took 480
json-stringify-safe took 656
small object at 100000 iterations
jsan took 48
CircularJSON took 153
json-stringify-safe took 502
jsan took 49
CircularJSON took 259
json-stringify-safe took 351
self referencing small object at 100000 iterations
jsan took 47
CircularJSON took 157
json-stringify-safe took 519
jsan took 58
CircularJSON took 245
json-stringify-safe took 367
small array at 100000 iterations
jsan took 46
CircularJSON took 138
json-stringify-safe took 531
jsan took 55
CircularJSON took 312
json-stringify-safe took 342
self referencing small array at 100000 iterations
jsan took 44
CircularJSON took 129
json-stringify-safe took 536
jsan took 51
CircularJSON took 310
json-stringify-safe took 313
string at 1000000 iterations
jsan took 281
CircularJSON took 283
json-stringify-safe took 429
jsan took 314
CircularJSON took 397
json-stringify-safe took 550
number at 1000000 iterations
jsan took 219
CircularJSON took 192
json-stringify-safe took 328
jsan took 252
CircularJSON took 314
json-stringify-safe took 462
null at 1000000 iterations
jsan took 209
CircularJSON took 192
json-stringify-safe took 339
jsan took 247
CircularJSON took 294
json-stringify-safe took 448
global at 1000 iterations
jsan took 1312
CircularJSON took 1073
json-stringify-safe took 1822
jsan took 21
CircularJSON took 846
json-stringify-safe took 841
decycledGlobal at 1000 iterations
jsan took 123
CircularJSON took 525
json-stringify-safe took 1221
jsan took 144
CircularJSON took 687
json-stringify-safe took 607
empty object at 1000000 iterations
jsan took 351
CircularJSON took 573
json-stringify-safe took 722
jsan took 374
CircularJSON took 827
json-stringify-safe took 1043
empty array at 1000000 iterations
jsan took 363
CircularJSON took 336
json-stringify-safe took 501
jsan took 399
CircularJSON took 456
json-stringify-safe took 628
small object at 100000 iterations
jsan took 48
CircularJSON took 171
json-stringify-safe took 570
jsan took 54
CircularJSON took 236
json-stringify-safe took 341
self referencing small object at 100000 iterations
jsan took 48
CircularJSON took 165
json-stringify-safe took 584
CircularJSON took 244
json-stringify-safe took 369
small array at 100000 iterations
jsan took 49
CircularJSON took 142
json-stringify-safe took 564
CircularJSON took 299
json-stringify-safe took 312
self referencing small array at 100000 iterations
jsan took 48
CircularJSON took 143
json-stringify-safe took 587
jsan took 51
CircularJSON took 279
json-stringify-safe took 300
string at 1000000 iterations
jsan took 274
CircularJSON took 314
json-stringify-safe took 431
jsan took 288
CircularJSON took 355
json-stringify-safe took 509
number at 1000000 iterations
jsan took 217
CircularJSON took 193
json-stringify-safe took 333
jsan took 232
CircularJSON took 244
json-stringify-safe took 412
null at 1000000 iterations
jsan took 214
CircularJSON took 206
json-stringify-safe took 328
jsan took 247
CircularJSON took 245
json-stringify-safe took 385
global at 1000 iterations
jsan took 1313
CircularJSON took 1107
json-stringify-safe took 1706
jsan took 22
CircularJSON took 832
json-stringify-safe took 776
decycledGlobal at 1000 iterations
jsan took 121
CircularJSON took 528
json-stringify-safe took 1113
jsan took 132
CircularJSON took 616
json-stringify-safe took 558
empty object at 1000000 iterations
jsan took 345
CircularJSON took 490
json-stringify-safe took 635
jsan took 339
CircularJSON took 859
json-stringify-safe took 963
empty array at 1000000 iterations
jsan took 354
CircularJSON took 333
json-stringify-safe took 462
jsan took 376
CircularJSON took 451
json-stringify-safe took 621
small object at 100000 iterations
jsan took 52
CircularJSON took 148
json-stringify-safe took 493
jsan took 55
CircularJSON took 259
json-stringify-safe took 357
self referencing small object at 100000 iterations
jsan took 53
CircularJSON took 159
json-stringify-safe took 493
jsan took 48
CircularJSON took 238
json-stringify-safe took 336
small array at 100000 iterations
jsan took 45
CircularJSON took 132
json-stringify-safe took 531
jsan took 47
CircularJSON took 274
json-stringify-safe took 291
self referencing small array at 100000 iterations
jsan took 47
CircularJSON took 127
json-stringify-safe took 505
jsan took 52
CircularJSON took 281
json-stringify-safe took 312
string at 1000000 iterations
jsan took 272
CircularJSON took 292
json-stringify-safe took 433
jsan took 325
CircularJSON took 361
json-stringify-safe took 522
number at 1000000 iterations
jsan took 227
CircularJSON took 200
json-stringify-safe took 331
jsan took 266
CircularJSON took 243
json-stringify-safe took 424
null at 1000000 iterations
jsan took 214
CircularJSON took 205
json-stringify-safe took 326
jsan took 264
CircularJSON took 266
json-stringify-safe took 421
global at 1000 iterations
jsan took 1329
CircularJSON took 1071
json-stringify-safe took 1727
jsan took 26
CircularJSON took 840
json-stringify-safe took 779
decycledGlobal at 1000 iterations
jsan took 130
CircularJSON took 519
json-stringify-safe took 1162
jsan took 152
CircularJSON took 645
json-stringify-safe took 601
empty object at 1000000 iterations
jsan took 340
CircularJSON took 485
json-stringify-safe took 674
jsan took 383
CircularJSON took 891
json-stringify-safe took 1058
empty array at 1000000 iterations
jsan took 352
CircularJSON took 327
json-stringify-safe took 470
jsan took 400
CircularJSON took 476
json-stringify-safe took 627
small object at 100000 iterations
jsan took 50
CircularJSON took 151
json-stringify-safe took 510
jsan took 52
CircularJSON took 270
json-stringify-safe took 384
self referencing small object at 100000 iterations
jsan took 55
CircularJSON took 161
json-stringify-safe took 517
jsan took 53
CircularJSON took 259
json-stringify-safe took 400
small array at 100000 iterations
jsan took 49
CircularJSON took 131
json-stringify-safe took 513
jsan took 50
CircularJSON took 314
json-stringify-safe took 348
self referencing small array at 100000 iterations
jsan took 44
CircularJSON took 141
json-stringify-safe took 545
jsan took 48
CircularJSON took 292
json-stringify-safe took 316
string at 1000000 iterations
jsan took 262
CircularJSON took 293
json-stringify-safe took 403
jsan took 305
CircularJSON took 360
json-stringify-safe took 504
number at 1000000 iterations
jsan took 227
CircularJSON took 187
json-stringify-safe took 324
jsan took 249
CircularJSON took 254
json-stringify-safe took 395
null at 1000000 iterations
jsan took 209
CircularJSON took 194
json-stringify-safe took 320
jsan took 224
CircularJSON took 258
json-stringify-safe took 406

@@ -17,4 +17,4 @@ var assert = require('assert');

it('uses the toJSON() method when possible', function() {
var obj = { toJSON: function() { return 'foobar' } };
assert.equal(jsan.stringify(obj), '"foobar"');
var obj = { a: { b: 1, toJSON: function(key) { return key } } };
assert.equal(jsan.stringify(obj, null, null, false), '{"a":"a"}');
});

@@ -62,2 +62,12 @@

if (typeof Symbol !== 'undefined') {
it('can handle ES symbols', function() {
var obj = {
s: Symbol('a')
}
var str = jsan.stringify(obj, null, null, true);
assert.deepEqual(str, '{"s":{"$jsan":"sa"}}');
});
}
it('works on objects with circular references', function() {

@@ -125,7 +135,16 @@ var obj = {};

str = '{"e":{"$jsan":"e:("}}';
var obj = jsan.parse(str);
assert(obj.e instanceof Error && obj.e.message === ':(');
var obj = jsan.parse(str);
assert(obj.e instanceof Error && obj.e.message === ':(');
});
if (typeof Symbol !== 'undefined') {
it('can decode ES symbols', function() {
str = '{"s1":{"$jsan":"sfoo"}, "s2":{"$jsan":"s"}}';
var obj = jsan.parse(str);
assert(typeof obj.s1 === 'symbol' && obj.s1.toString() === 'Symbol(foo)');
assert(typeof obj.s2 === 'symbol' && obj.s2.toString() === 'Symbol()');
});
}
it('works on object strings with a circular dereferences', function() {

@@ -132,0 +151,0 @@ var str = '{"a":1,"b":"string","c":[2,3],"d":null,"self":{"$jsan":"$"}}';

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc