Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hashable JSON (only stringify)
(version: 0)
Comparing performance of:
Current implementation vs json-stable-stringify vs fast-json-stable-stringify vs david-stringify vs json-canonicalization
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { za: { wannabe: [ "Yo, I'll tell you what I want, what I really, really want", "So tell me what you want, what you really, really want", "I'll tell you what I want, what I really, really want", "So tell me what you want, what you really, really want", "I wanna, (ha) I wanna, (ha) I wanna, (ha) I wanna, (ha)", "I wanna really, really, really wanna zigazig ah", ], }, 12: 12, "": "empty", abcd: { objAsArray: { 0: { n: "val", e: "val" }, 1: { n: "val", e: "val" }, 2: { n: "val", e: "val" }, 3: { n: "val", e: "val" }, 4: { n: "val", e: "val" }, 5: { n: "val", e: "val" }, 6: { n: "val", e: "val" }, 7: { n: "val", e: "val" }, 8: { n: "val", e: "val" }, 9: { n: "val", e: "val" }, 10: { n: "val", e: "val", r: { n: 12, 3: 3 } }, }, l: { id: "asdfas" }, array: [ 12, 3, 13, 32, 134, 1234, 5, 213, 2, 34, 5, 134, 0, 51, 234124, 12, 51, 235, 123, 4, 32, 42, 5, 235, 1235, 1253, ], "😄": "💩", "-0": 123, }, }; function customcanonicalize(_, value) { if (value === null || typeof value !== 'object' || Array.isArray(value)) { return value; } var keys = Object.keys(value).sort(); var length = keys.length; var object = {}; for (var i = 0; i < length; i++) { object[keys[i]] = value[keys[i]]; } return object; } function customStringify(obj) { return JSON.stringify(obj, customcanonicalize); }; var canonicalize = function(object) { var buffer = ''; serialize(object); return buffer; function serialize(object) { if (object !== null && typeof object === 'object') { if (Array.isArray(object)) { buffer += '['; let next = false; // Array - Maintain element order object.forEach((element) => { if (next) { buffer += ','; } next = true; // Recursive call serialize(element); }); buffer += ']'; } else { buffer += '{'; let next = false; // Object - Sort properties before serializing Object.keys(object).sort().forEach((property) => { if (next) { buffer += ','; } next = true; // Properties are just strings - Use ES6 buffer += JSON.stringify(property); buffer += ':'; // Recursive call serialize(object[property]); }); buffer += '}'; } } else { // Primitive data type - Use ES6 buffer += JSON.stringify(object); } } }; var json = typeof JSON !== 'undefined' ? JSON : require('jsonify'); function stableStringify(obj, opts) { if (!opts) opts = {}; if (typeof opts === 'function') opts = { cmp: opts }; var space = opts.space || ''; if (typeof space === 'number') space = Array(space+1).join(' '); var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false; var replacer = opts.replacer || function(key, value) { return value; }; var cmp = opts.cmp && (function (f) { return function (node) { return function (a, b) { var aobj = { key: a, value: node[a] }; var bobj = { key: b, value: node[b] }; return f(aobj, bobj); }; }; })(opts.cmp); var seen = []; return (function stringify (parent, key, node, level) { var indent = space ? ('\n' + new Array(level + 1).join(space)) : ''; var colonSeparator = space ? ': ' : ':'; if (node && node.toJSON && typeof node.toJSON === 'function') { node = node.toJSON(); } node = replacer.call(parent, key, node); if (node === undefined) { return; } if (typeof node !== 'object' || node === null) { return json.stringify(node); } if (isArray(node)) { var out = []; for (var i = 0; i < node.length; i++) { var item = stringify(node, i, node[i], level+1) || json.stringify(null); out.push(indent + space + item); } return '[' + out.join(',') + indent + ']'; } else { if (seen.indexOf(node) !== -1) { if (cycles) return json.stringify('__cycle__'); throw new TypeError('Converting circular structure to JSON'); } else seen.push(node); var keys = objectKeys(node).sort(cmp && cmp(node)); var out = []; for (var i = 0; i < keys.length; i++) { var key = keys[i]; var value = stringify(node, key, node[key], level+1); if(!value) continue; var keyValue = json.stringify(key) + colonSeparator + value; ; out.push(indent + space + keyValue); } seen.splice(seen.indexOf(node), 1); return '{' + out.join(',') + indent + '}'; } })({ '': obj }, '', obj, 0); }; var isArray = Array.isArray || function (x) { return {}.toString.call(x) === '[object Array]'; }; var objectKeys = Object.keys || function (obj) { var has = Object.prototype.hasOwnProperty || function () { return true }; var keys = []; for (var key in obj) { if (has.call(obj, key)) keys.push(key); } return keys; }; function fastStableStringify(data, opts) { if (!opts) opts = {}; if (typeof opts === 'function') opts = { cmp: opts }; var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false; var cmp = opts.cmp && (function (f) { return function (node) { return function (a, b) { var aobj = { key: a, value: node[a] }; var bobj = { key: b, value: node[b] }; return f(aobj, bobj); }; }; })(opts.cmp); var seen = []; return (function stringify (node) { if (node && node.toJSON && typeof node.toJSON === 'function') { node = node.toJSON(); } if (node === undefined) return; if (typeof node == 'number') return isFinite(node) ? '' + node : 'null'; if (typeof node !== 'object') return JSON.stringify(node); var i, out; if (Array.isArray(node)) { out = '['; for (i = 0; i < node.length; i++) { if (i) out += ','; out += stringify(node[i]) || 'null'; } return out + ']'; } if (node === null) return 'null'; if (seen.indexOf(node) !== -1) { if (cycles) return JSON.stringify('__cycle__'); throw new TypeError('Converting circular structure to JSON'); } var seenIndex = seen.push(node) - 1; var keys = Object.keys(node).sort(cmp && cmp(node)); out = ''; for (i = 0; i < keys.length; i++) { var key = keys[i]; var value = stringify(node[key]); if (!value) continue; if (out) out += ','; out += JSON.stringify(key) + ':' + value; } seen.splice(seenIndex, 1); return '{' + out + '}'; })(data); }; function davidStringify(obj) { const allObjectKeys = getAllKeys(obj); return JSON.stringify(obj, allObjectKeys.sort()); } function getAllKeys(obj) { let keys = []; Object.entries(obj).forEach(([key, value]) => { keys.push(key); if (value && typeof value === 'object') { keys = keys.concat(getAllKeys(value)); } }); return keys; }
Tests:
Current implementation
customStringify(obj)
json-stable-stringify
stableStringify(obj)
fast-json-stable-stringify
fastStableStringify(obj)
david-stringify
davidStringify(obj)
json-canonicalization
canonicalize(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Current implementation
json-stable-stringify
fast-json-stable-stringify
david-stringify
json-canonicalization
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
To address the provided code snippet and benchmark results, let's break down the key components and analyze them: 1. **Code Snippet**: The given JavaScript code appears to be related to stringification of objects, including implementations of `customStringify`, `stableStringify`, `fastStableStringify`, and `davidStringify`. These functions seem to be designed for different use cases or optimizations. 2. **Benchmark Results**: The benchmark results show the performance (in executions per second) of each test case on a Chrome 96 browser with Mac OS X 10.15.7 operating system. There are four test cases: - `Current implementation` - `json-stable-stringify` (which likely uses the `JSON.stringify()` method) - `fast-json-stable-stringify` (optimized for performance, possibly using a more efficient algorithm or caching mechanisms) - `davidStringify` 3. **Analysis and Observations**: - The `Current implementation` outperforms `json-stable-stringify`, indicating that the custom implementation might be optimized or more efficient in some way. - `fast-json-stable-stringify` significantly outperforms both `Current implementation` and `json-stable-stringify`. This could suggest that the optimization provided by `fast-json-stable-stringify` is substantial, possibly due to a better algorithm, caching, or other performance-enhancing techniques. - The `davidStringify` implementation performs worse than all other test cases. However, without more context or information about the specific optimizations used in `json-stable-stringify` and `fast-json-stable-stringify`, it's challenging to determine why `davidStringify` is less efficient. 4. **Recommendations**: - Consider refactoring the code for `Current implementation` if possible, especially given its performance advantage over `json-stable-stringify`. - If not already implemented, consider analyzing and optimizing the `fast-json-stable-stringify` algorithm further to understand its advantages. - Investigate potential improvements or optimizations for `davidStringify`, possibly by reviewing its implementation against that of `Current implementation` and other test cases.
Related benchmarks:
Hashable JSON
Hashable JSON + md4
Hashable JSON 2.0 (only stringify)
Hashable JSON 3.0 (only stringify)
Comments
Confirm delete:
Do you really want to delete benchmark?