Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
native intersect vs lodash intersection
(version: 0)
Comparing performance of:
native vs lodash
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.15/lodash.min.js'></script>
Script Preparation code:
function intersect(...array) { return [...new Set(array.reduce((a, b) => a.filter(c => b.includes(c))))]; }
Tests:
native
intersect([1, 1, 3, 2, 2], [5, 2, 2, 1, 4], [2, 1, 1]);
lodash
_.intersection([1, 1, 3, 2, 2], [5, 2, 2, 1, 4], [2, 1, 1]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
native
lodash
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
native
1193564.6 Ops/sec
lodash
952823.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. **Benchmark Definition JSON** The provided JSON defines two benchmark tests: `native intersect vs lodash intersection`. The script preparation code for both benchmarks is as follows: * `intersect`: This function takes an array of numbers as input, reduces it to a set (using `Set` constructor), and then filters the original array to keep only the elements present in the set. It's essentially a custom implementation of intersection between two arrays. * The HTML preparation code includes a script tag that loads the Lodash library (version 4.17.15) from a CDN. **What is being tested?** The benchmark tests the performance of two approaches to calculate the intersection between two arrays: 1. **Native JavaScript**: The `intersect` function implemented above. 2. **Lodash Intersection Function**: A function imported from the Lodash library, specifically the `_intersection` function. **Options compared** These are the options being compared in the benchmark: * Native JavaScript implementation (`native`) * Lodash intersection function (`lodash`) **Pros and Cons of each approach:** 1. **Native JavaScript** * Pros: + Low overhead, as it only uses built-in JavaScript methods. + Fast execution time, since it avoids additional library dependencies. * Cons: + Requires manual implementation of the intersection logic, which can be error-prone. 2. **Lodash Intersection Function** * Pros: + Less code to write and maintain, as it's a pre-existing function in a popular library. + Often optimized for performance by the Lodash developers. * Cons: + Adds an external dependency (the Lodash library), which can introduce additional overhead. + May have slower execution time compared to the native implementation. **Library: Lodash** Lodash is a JavaScript utility library that provides a wide range of functions for various tasks, such as array manipulation, string manipulation, and more. The `_intersection` function is part of this library, which takes two arrays as input and returns their intersection (i.e., elements present in both arrays). **Special JS feature or syntax: None** There are no special JavaScript features or syntax used in these benchmarks. **Other alternatives** If you're interested in exploring alternative approaches to calculate the intersection between two arrays, here are a few options: * Using `Array.prototype.filter()` with an arrow function to create a new array containing only elements present in both input arrays. * Using `Set` and `Array.prototype.includes()` methods to achieve the same result. These alternatives might have similar performance characteristics to the native JavaScript implementation or Lodash intersection function, but may require additional setup or dependencies.
Related benchmarks:
lodash intersection vs JS
lodash intersectionWith vs array filtering with includes
intersectionWith vs find in find vs set small
Lodash vs. Set Intersection on pre-existing sets
Comments
Confirm delete:
Do you really want to delete benchmark?