<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
const a = [2, 1];
const b = [2, 3];
const result = _.intersection(a, b);
const a = [2, 1];
const b = [2, 3];
const intersection = (arr, args) =>
arr.filter(item => args.every(arr => arr.includes(item)))
const result = intersection(a, b);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
vanilla js |
Test name | Executions per second |
---|---|
lodash | 4949399.0 Ops/sec |
vanilla js | 47151260.0 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and other considerations.
Benchmark Overview
The benchmark compares two approaches to find the intersection of two arrays: one using Lodash (lodash
) and another written in vanilla JavaScript (vanilla js
).
What is tested?
intersection
function from Lodash is being used to find the common elements between two arrays.Options compared
The benchmark compares two options:
intersection
function.Pros and Cons of each approach
Lodash
Pros:
Cons:
Vanilla JavaScript
Pros:
Cons:
Library and its purpose
Lodash is a popular JavaScript utility library that provides a wide range of functions for various tasks, including array operations. In this case, the intersection
function is used to find the common elements between two arrays.
Special JS feature or syntax (not applicable)
There are no special features or syntaxes being tested in this benchmark. The focus is on comparing the performance and implementation differences between Lodash's pre-built function and a custom vanilla JavaScript implementation.
Other alternatives
If you're interested in exploring other approaches, here are some alternatives:
Array.prototype.filter()
with includes()
: This approach uses the filter()
method to create a new array containing only elements that meet the condition (i.e., being included in both arrays).lodash-es
(a subset of Lodash with ES6+ support) or moment.js
for date-related tasks.Keep in mind that each approach has its trade-offs, and the choice ultimately depends on your project's requirements, constraints, and your team's expertise.