Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Custom Array Equality Function vs Lodash.isEqualWith Equality Comparison for Shallow Array of Strings.
(version: 0)
Test on isEqualWith performance
Comparing performance of:
Arrays are equal vs _.isEqualWith
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
window.foo = ['cat', 'dog', 'bird']; window.bar = ['cat', 'dog', 'bird']; function arraysAreEqual( firstArray, secondArray, options, ) { if (firstArray.length !== secondArray.length) { return false; } const { comparator, orderSensitive = true } = options; const first = orderSensitive ? firstArray : [...firstArray.sort()]; const second = orderSensitive ? secondArray : [...secondArray.sort()]; return first.every((firstItem, index) => { const secondItem = second[index]; if (comparator != null) { return comparator(firstItem, secondItem); } return firstItem === secondItem; }); }
Tests:
Arrays are equal
arraysAreEqual(window.foo, window.bar, {comparator: (a, b) => a===b, orderSensitive: false})
_.isEqualWith
_.isEqualWith(window.foo, window.bar, (a, b) => a===b)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Arrays are equal
_.isEqualWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Arrays are equal
3168949.0 Ops/sec
_.isEqualWith
5066831.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help explain the benchmark and its results. **Benchmark Overview** The benchmark measures the performance of two different approaches for comparing arrays: `arraysAreEqual` (a custom implementation) and `_.isEqualWith` from the Lodash library. The test compares the equality of two shallow arrays of strings (`window.foo` and `window.bar`) using different comparison options. **Comparison Options** There are two main options being compared: 1. **Arrays are equal**: This option uses the default behavior of both `arraysAreEqual` and `_.isEqualWith`, which checks for reference equality, i.e., whether the arrays have the same underlying memory location. 2. **_.isEqualWith**: This option uses a custom comparator function provided by Lodash to compare the elements of the arrays. The comparator function is specified as `(a, b) => a === b`, which simply checks if both elements are equal using strict equality (`===`). **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Arrays are equal (default behavior)**: * Pros: Simple and straightforward to implement. * Cons: Can be slower due to reference comparison, which checks for memory location instead of actual value equality. 2. **_.isEqualWith**: * Pros: Can be faster since it uses a custom comparator function that only checks for value equality. * Cons: Requires Lodash library and can be more complex to implement. **Library - Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object transformation, and functional programming. In this benchmark, `_.isEqualWith` is used to compare the arrays using a custom comparator function. **Special JS Feature/Syntax - None** There are no special JavaScript features or syntaxes being used in this benchmark beyond the standard language. **Other Alternatives** If you were to reimplement this benchmark without using Lodash, you could consider using other libraries or implementing your own comparator function. Some alternatives include: * Using `Array.prototype.every()` and a custom callback function. * Implementing a custom comparator function similar to the one used in `_.isEqualWith`. * Utilizing a library like Jest or Mocha for benchmarking. Keep in mind that the choice of alternative will depend on your specific requirements and preferences.
Related benchmarks:
Lodash.isEqual vs Array.toString() Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs Array.join() Equality Comparison for Shallow Array of Strings.
Native filter includes vs_.isEqual Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs join Equality Comparison for Shallow Array of Strings.
Lodash.isEqual vs Lodash.isEqualWith Equality Comparison for Shallow Array of Strings.
Comments
Confirm delete:
Do you really want to delete benchmark?