Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test on isEqual and fastEqual false performance
(version: 2)
Comparing performance of: _.isEqual vs JSON.stringify
Comparing performance of:
_.isEqual vs JSON.stringify vs fastEqual
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
window.foo = { arr: [1, 3], num: 12, o: { name: 'foo' }, quantum: [{ spin: 'x' }, { spin: 'y' }]}; window.bar = { arr: [1, 2], num: 12, o: { name: 'foo' }, quantum: [{ spin: 'x' }, { spin: 'y' }]}; var isArray = Array.isArray; var keyList = Object.keys; var hasProp = Object.prototype.hasOwnProperty; function fastEqual(a, b) { if (a === b) return true; if (a && b && typeof a == 'object' && typeof b == 'object') { var arrA = isArray(a) , arrB = isArray(b) , i , length , key; if (arrA && arrB) { length = a.length; if (length != b.length) return false; for (i = length; i-- !== 0;) if (!fastEqual(a[i], b[i])) return false; return true; } if (arrA != arrB) return false; var dateA = a instanceof Date , dateB = b instanceof Date; if (dateA != dateB) return false; if (dateA && dateB) return a.getTime() == b.getTime(); var regexpA = a instanceof RegExp , regexpB = b instanceof RegExp; if (regexpA != regexpB) return false; if (regexpA && regexpB) return a.toString() == b.toString(); var keys = keyList(a); length = keys.length; if (length !== keyList(b).length) return false; for (i = length; i-- !== 0;) if (!hasProp.call(b, keys[i])) return false; for (i = length; i-- !== 0;) { key = keys[i]; if (!fastEqual(a[key], b[key])) return false; } return true; } return a!==a && b!==b; }; window.fastEqual = fastEqual;
Tests:
_.isEqual
_.isEqual(window.foo, window.bar)
JSON.stringify
JSON.stringify(window.foo) === JSON.stringify(window.bar);
fastEqual
window.fastEqual(window.foo, window.bar);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
_.isEqual
JSON.stringify
fastEqual
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):
The provided JSON represents a JavaScript microbenchmark on MeasureThat.net, which compares the performance of three functions: `_.isEqual` from the Lodash library, `JSON.stringify`, and a custom implementation called `fastEqual`. **What is being tested?** The benchmark tests the performance of these three functions in comparing two objects, `window.foo` and `window.bar`. The comparison includes checks for: * Deep object equality (e.g., arrays, objects) * Date comparisons * RegExp comparisons * String comparisons **Options compared:** 1. **_.isEqual**: A function from the Lodash library that performs deep object equality. 2. **JSON.stringify**: A built-in JavaScript function that converts an object to a string representation. 3. **fastEqual**: A custom implementation provided in the benchmark code, which uses various checks to compare two objects. **Pros and Cons of each approach:** 1. **_.isEqual**: Pros: * Fast and efficient for deep object comparisons * Handles complex data structures like arrays and objects Cons: * Dependent on the Lodash library, which may not be suitable for all use cases 2. **JSON.stringify**: Pros: + Easy to implement and understand + Works well for simple object comparisons Cons: + Inefficient for deep object comparisons (as it creates a new string representation) + Not suitable for comparing complex data structures like arrays and objects 3. **fastEqual**: Pros: * Custom implementation that can be optimized for specific use cases * Handles various data types, including arrays, objects, dates, and RegExps Cons: * More complex and harder to understand than _.isEqual or JSON.stringify + Requires more CPU cycles due to the custom implementation **Other considerations:** * The `fastEqual` implementation uses a combination of checks to compare two objects. While it provides a high degree of accuracy, it may not be suitable for all use cases. * The benchmark results show that `_.isEqual` is the fastest function in this comparison, followed by `JSON.stringify`, and then `fastEqual`. **Alternative approaches:** 1. **ES6 Object Compare**: If you're using modern JavaScript versions, you can use the built-in `Object.is()` method to compare objects. 2. **Recursive Object Comparison Libraries**: There are libraries like `compare-objects` or `deep-equal` that provide more advanced object comparison features. In summary, the choice of which function to use depends on your specific requirements and performance constraints. If you need a simple and efficient way to compare objects, _.isEqual might be a good option. However, if you need more control over the comparison process or are dealing with complex data structures, fastEqual or custom implementation might be more suitable.
Related benchmarks:
Lodash.isEqual vs JSON.stringify Equality Comparison for Object
Lodash.isEqual vs JSON.stringify Equality Comparison for deep objects.
Lodash.isEqual vs JSON.stringify Equality Comparison for Array of objects
Lodash.isEqual vs JSON.stringify Equality Comparison for simple objects
Comments
Confirm delete:
Do you really want to delete benchmark?