<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var keywords = new Array(3000).fill({}).map((_, i) => ({id: i}));
var selectedKeywords = new Array(100).fill({}).map((_, i) => ({id: i}));
let count1 = _.filter(keywords, keyword => _.some(selectedKeywords, sk => sk.id === keyword.id)).length;
let count2 = keywords.filter(keyword => {
selectedKeywords.some(sk => sk.id === keyword.id);
}).length;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash | |
native |
Test name | Executions per second |
---|---|
lodash | 1377.8 Ops/sec |
native | 2749.2 Ops/sec |
Measuring JavaScript performance is an essential aspect of web development, and MeasureThat.net provides a unique platform for comparing the efficiency of different approaches.
The provided JSON represents a benchmark definition with two test cases: "native" and "lodash". The script preparation code is identical for both test cases, but they differ in their execution paths.
Test Case 1: Lodash (.filter()
method)
let count1 = _.filter(keywords, keyword => _.some(selectedKeywords, sk => sk.id === keyword.id)).length;
In this test case, the lodash
library is used to filter the keywords
array. The _filter()
method iterates over the keywords
array and checks each element against a predicate function that uses the _.some()
method to search for matching elements in the selectedKeywords
array. The _.some()
method returns true
if at least one element in the selectedKeywords
array has an id
property matching the current keyword's id
. If any of these conditions are true, the predicate function returns true
, and the element is included in the filtered result.
Test Case 2: Native (.filter()
method without Lodash)
let count2 = keywords.filter(keyword => {
selectedKeywords.some(sk => sk.id === keyword.id);
}).length;
In this test case, a native implementation of the _.filter()
method is used. This implementation iterates over the keywords
array and checks each element against a predicate function that uses the some()
method to search for matching elements in the selectedKeywords
array. The some()
method returns true
if at least one element in the selectedKeywords
array has an id
property matching the current keyword's id
. If any of these conditions are true, the predicate function returns true
, and the element is included in the filtered result.
Comparison of Options
Library Used: Lodash is a popular utility library that provides a comprehensive set of functions for functional programming, array manipulation, and other common tasks. In this benchmark, the _filter()
method is used to filter the keywords
array based on conditions defined in the predicate function.
Special JS Feature/Syntax: None mentioned in this explanation.
The alternative approaches would be:
map()
, forEach()
, or reduce()
.Keep in mind that these alternatives would require additional research and development to achieve comparable results with Lodash or the native implementation.