Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Run results for:
Hashable JSON + md4
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/119.0
Browser:
Firefox 119
Operating system:
Linux
Device Platform:
Desktop
Date tested:
2 years ago
Test name
Executions per second
Current implementation
59245.2 Ops/sec
json-stable-stringify
38850.4 Ops/sec
fast-json-stable-stringify
52463.6 Ops/sec
david-stringify
32868.2 Ops/sec
json-canonicalization
47023.8 Ops/sec
HTML Preparation code:
<script src="https://unpkg.com/js-md4"></script>
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
md4(customStringify(obj))
json-stable-stringify
md4(stableStringify(obj))
fast-json-stable-stringify
md4(fastStableStringify(obj))
david-stringify
md4(davidStringify(obj))
json-canonicalization
md4(canonicalize(obj))