Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object comparison
(version: 0)
Comparing performance of:
lodash vs underscore vs JSON.stringify() JSON.st vs isEqualShallow()
Created:
9 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script> <script> var underscore = _.noConflict(); </script> <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.0/lodash.min.js"></script> <script> var lodash = _.noConflict(); </script> <script> var obj1 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: 5, upper: 10 }, channels: 'ZWIP' }, result: null }; var obj2 = { properties: { selectionName: null, price: { format: 'fractional', odds: null }, status: 'A', display: { displayed: 'Y', order: null }, selectionScore: { home: null, away: null }, band: { lower: null, upper: null }, channels: 'ZWIP' }, result: null }; lodash = window.lodash; underscore = window.underscore window.isEqualShallow = function (a, b) { if (!a && !b) { return true; } if (!a && b || a && !b) { return false; } var numKeysA = 0, numKeysB = 0, key; for (key in b) { numKeysB++; if (/* !isPrimitive(b[key]) || */ !a.hasOwnProperty(key) || (a[key] !== b[key])) { // console.log(key, a, b); return false; } } for (key in a) { numKeysA++; } return numKeysA === numKeysB; } </script>
Tests:
lodash
window.lodash.isEqual(obj1, obj2);
underscore
window.underscore.isEqual(obj1, obj2);
JSON.stringify() JSON.st
JSON.stringify(obj1) == JSON.stringify(obj2);
isEqualShallow()
window.isEqualShallow(obj1, obj2);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
lodash
underscore
JSON.stringify() JSON.st
isEqualShallow()
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):
Measuring JavaScript performance is crucial in today's fast-paced web development world. Let's dive into the benchmark and explain what's being tested, compared, pros and cons of each approach, and other considerations. **Benchmark Definition** The benchmark measures the time taken to compare two objects using different methods. The test cases are: 1. `window.lodash.isEqual(obj1, obj2);` 2. `window.underscore.isEqual(obj1, obj2);` 3. `JSON.stringify(obj1) == JSON.stringify(obj2);` 4. `window.isEqualShallow(obj1, obj2);` **Tested Options** The benchmark compares four options for comparing two objects: ### 1. Lodash (`window.lodash.isEqual(obj1, obj2);`) Lodash is a popular JavaScript library that provides a wide range of functions for tasks such as string manipulation, array manipulation, and more. In this case, `isEqual` is used to compare two objects. Pros: * Widely used and well-maintained * Provides a robust implementation for object comparison Cons: * Adds an extra dependency (Lodash) to the test environment * May have performance overhead due to the library's complexity ### 2. Underscore (`window.underscore.isEqual(obj1, obj2);`) Underscore is another popular JavaScript library that provides a set of functional programming helpers. In this case, `isEqual` is used for object comparison. Pros: * Similar to Lodash, widely used and well-maintained * Provides a robust implementation for object comparison Cons: * Adds an extra dependency (Underscore) to the test environment * May have performance overhead due to the library's complexity ### 3. Stringification (`JSON.stringify(obj1) == JSON.stringify(obj2);`) This approach uses stringification, converting both objects to JSON strings and comparing them for equality. Pros: * Lightweight and simple to implement * Does not add any extra dependencies Cons: * May not work correctly with complex object structures or nested arrays * Can be slower than native object comparison methods due to the overhead of JSON parsing ### 4. Custom implementation (`window.isEqualShallow(obj1, obj2);`) This approach uses a custom implementation that manually compares the properties of two objects. Pros: * Lightweight and simple to implement * Does not add any extra dependencies Cons: * May be less robust than commercial library implementations (Lodash or Underscore) * Can be slower than native object comparison methods due to manual implementation **Other Considerations** When choosing an approach for object comparison in JavaScript, consider the following factors: * Performance: Native object comparison methods are generally faster than stringification or custom implementations. * Robustness: Commercial library implementations (Lodash or Underscore) provide more robust and reliable results compared to custom implementations. * Weight: Lightweight approaches like stringification may be suitable for small projects or applications with limited resources. **Benchmark Results** The benchmark results show the performance of each option on a Mac OS X 10.14.2 system, running Chrome 71. The `isEqualShallow` implementation performs best, followed by Lodash and Underscore. Stringification has the lowest performance due to its simplicity and potential for errors. Keep in mind that these results may vary depending on your specific use case, project requirements, and target audience.
Related benchmarks:
Compare comparison with null or undefined
Lodash values vs Object.values
object deep comparison
Compare comparison with null or undefined. With typeof
Compare comparison with null or undefined (updated)
Comments
Confirm delete:
Do you really want to delete benchmark?