<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-min.js"></script>
var underscore = _.noConflict();
var objectArrays = [
['bedroom', 'kitchen', 'bathroom', 'living room', 'indoors', 'outdoors', 'city', 'urban', 'villa', 'hotel'],
['bed', 'kitchen island', 'bath', 'room', 'city'],
['bed', 'bathroom', 'bath', 'room', 'city']
];
let shared = [];
objectArrays.forEach((array, i) => {
if (i === 0) {
shared.push(array);
}
else {
_.intersection(shared, array);
}
});
let shared = [];
objectArrays.forEach((array, i) => {
if (i === 0) {
shared.push(array);
}
else {
underscore.intersection(shared, array);
}
});
const arrayLodash = _.intersection(objectArrays);
const arrayUnderscore = underscore.intersection(objectArrays);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
manual lodash | |
manual underscore | |
Lodash | |
Underscore |
Test name | Executions per second |
---|---|
manual lodash | 447775.0 Ops/sec |
manual underscore | 624814.6 Ops/sec |
Lodash | 866699.6 Ops/sec |
Underscore | 1333295.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on this provided JSON.
Benchmark Overview
The benchmark compares the performance of three approaches to find the intersection of two arrays:
intersection
operation from scratch without using any libraries.lodash
library, which provides a robust set of utility functions for JavaScript development.underscore
library, which is similar to lodash
but has some differences in its API.Options Compared
The benchmark compares the performance of each approach on two identical sets of input arrays:
objectArrays
: An array of two arrays, each containing a mix of strings and numbers.The comparison is done using the same test case with different implementations: one for manual implementation, one for Lodash, one for Underscore, and another for manual implementation again (with a slight variation).
Pros and Cons
Library Descriptions
Special JS Features or Syntax
There are no special JavaScript features or syntax used in this benchmark. The code only uses standard JavaScript features like arrays, loops, and function calls.
Other Alternatives
If you're interested in exploring other approaches to find the intersection of two arrays, here are some alternatives:
Set
objects: You can convert both arrays to sets and then use the intersection
method to find the common elements.I hope this explanation helps you understand what's being tested in this benchmark!