Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash vs json-stable-stringify (Stringify) "a" === "b"
(version: 0)
Used json-stable-stringify (Stringify) version 1.0.2 from https://cdn.jsdelivr.net/npm/json-stable-stringify@1.0.2/index.js
Comparing performance of:
_.isEqual vs json-stable-stringify
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Script Preparation code:
var json = typeof JSON !== 'undefined' ? JSON : console.error("JSON SUPPORT REQUIRED"); 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 jsonStableStringify(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 + ']'; } 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)); };
Tests:
_.isEqual
_.isEqual("a", "b");
json-stable-stringify
jsonStableStringify("a") === jsonStableStringify("b")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.isEqual
json-stable-stringify
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.isEqual
205258816.0 Ops/sec
json-stable-stringify
7737154.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and explain what's being tested, compared, and its pros and cons. **Benchmark Definition** The benchmark measures two functions: Lodash's `isEqual` function and json-stable-stringify (JSST) version 1.0.2's stringification functionality. **Lodash's `isEqual` Function** Lodash's `isEqual` function is a utility function that checks if two values are equal, considering primitive types and nested objects. Pros: * Efficiently handles complex object comparisons. * Can handle different data types, such as numbers, strings, booleans, and arrays. Cons: * May be slower for very large objects due to its recursive nature. * Requires the Lodash library, which may add overhead. **json-stable-stringify (JSST) Stringification Functionality** The JSST stringification function is designed to produce a stable string representation of JavaScript objects, including cyclic references. It's optimized for performance and handles various edge cases. Pros: * Produces a stable and consistent string representation. * Handles cyclic references efficiently. * Can be more efficient than other stringification methods for large objects. Cons: * May produce larger strings compared to other stringification methods. * Requires careful optimization to handle certain edge cases, such as nested arrays or objects with complex properties. **Comparison** The benchmark compares the performance of Lodash's `isEqual` function and JSST's stringification functionality on two specific inputs: `"a" === "b"` (which should always be false) and an empty object. **Other Considerations** * The benchmark uses a virtual user agent (UA) to simulate real-world usage patterns. This helps to provide more realistic results. * The executions per second metric gives an idea of the performance differences between the two functions. * The raw UA string, browser, device platform, operating system, and executions per second provide additional context about the test environment. **Alternatives** Other alternatives for comparing string equality or object comparisons include: * `===` operator (native JavaScript) * `Object.is()` function (native JavaScript) * Other libraries or frameworks that provide similar functionality to Lodash's `isEqual` or JSST's stringification Note that these alternatives may have different performance characteristics, pros, and cons compared to the benchmarked functions.
Related benchmarks:
lodash.keys vs Object.keys
lodash.keys vs Object.keys
lodash.keys vs Object.keys
Lodash deep clone vs JSON.stringfy
isEmpty vs Object.keys
Comments
Confirm delete:
Do you really want to delete benchmark?