Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
DifferenceBy vs native
(version: 0)
Comparing performance of:
_.differenceBy vs Native
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array1 = [{name: 'foo', value: 3},{name:'bar', value: 4},{name:'x', value: 0}] var array2 = [{name: 'foo', type: 'a'},{name:'baz', type:'b'},{name:'x', type:'c'}]
Tests:
_.differenceBy
var difference = _.differenceBy(array1, array2, 'name')
Native
if (array1) { if (array2) { var difference = [array1.map(a => a.name), array2.map(a => a.name)] .reduce((a, b) => a.filter(c => !b.includes(c))) } else { array1 } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.differenceBy
Native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.differenceBy
2726302.2 Ops/sec
Native
7720903.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that tests two approaches to find the difference between two arrays: `_.differenceBy` from the Lodash library and a native implementation. **Test Cases** There are two test cases: 1. **_.differenceBy**: This test case uses the Lodash library's `differenceBy` function, which takes three arguments: * The first array (`array1`) * The second array (`array2`) * A key function (`'name'`) that extracts the common property from both objects The test case prepares two arrays, `array1` and `array2`, with different values for the `'name'` property. 2. **Native**: This test case implements the difference-by algorithm manually using JavaScript. It uses a map operation to create an array of names from both input arrays, and then filters out the common elements using another filter method. **Library: Lodash** Lodash is a popular JavaScript library that provides a collection of reusable functions for various tasks, such as utility functions, array operations, and more. The `differenceBy` function is part of this library and allows you to find the difference between two arrays based on a common property. **Pros of Using Lodash (`_.differenceBy`)** * Readability: The implementation is concise and easy to understand, thanks to the high-quality documentation provided by Lodash. * Performance: Lodash's implementation is optimized for performance, using techniques like caching and memoization to avoid unnecessary computations. * Maintainability: By using a well-maintained library, you can focus on other aspects of your project rather than rewriting common algorithms. **Cons of Using Lodash (`_.differenceBy`)** * Additional dependency: Your project needs to include the Lodash library, which might add overhead in terms of download size and loading time. * Potential performance impact: Although Lodash's implementation is optimized, it still introduces an extra layer of indirection that might affect performance. **Pros of Native Implementation** * No additional dependencies: Your project doesn't need to include any external libraries or dependencies. * Customization: You can modify the algorithm to suit your specific needs and requirements. **Cons of Native Implementation** * Increased complexity: The implementation is more complex, requiring a deeper understanding of JavaScript's array operations and filtering techniques. * Potential performance impact: Without optimizations, the native implementation might be slower than Lodash's optimized version. **Other Alternatives** If you prefer not to use Lodash or want to explore other options, here are some alternatives: 1. `Array.prototype.filter()`: You can achieve a similar result using the `filter()` method on both input arrays, like this: `[array1.map(a => a.name), array2.map(a => a.name)].reduce((a, b) => a.filter(c => !b.includes(c)))`. 2. `Set` and `intersection()`: Another approach is to use two sets (one for each array) and compute their intersection using the `intersection()` method. 3. Libraries like `fast-async-mathlib`, which provides optimized implementations of various mathematical operations, including set theory functions. In summary, both approaches have their pros and cons. If you need readability and performance, Lodash's `differenceBy` is a good choice. However, if you prefer to avoid dependencies or customize the algorithm, the native implementation might be a better fit.
Related benchmarks:
Array.from 1e6 vs 1e3
Spread vs Push to Same Array v1
Array.from vs Array spread with mapping of values
Array#concat vs Array#push
new array vs array.length = 0
Comments
Confirm delete:
Do you really want to delete benchmark?