<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var fs = {
f1: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f2: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f3: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f4: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f5: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f6: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
},
f7: {
A: ["id1", "id2"],
B: ["id1", "id2"],
C: ["id1", "id2"],
D: ["id1", "id2"],
E: ["id1", "id2"],
F: ["id1", "id2"],
}
}
const e = (f, i, d) => {
const c = _.get(fs, [f, i]);
return c?.includes(d);
};
e("f7", "F", "id2")
const e = (f, i, d) => {
const c = _.get(fs, [f, i]);
return _.includes(c, d);
};
e("f7", "F", "id2")
const e = (f, i, d) => {
const gf = fs[f];
const gfc = gf ? gf[i] : [];
const fd = gfc?.includes(d);
return fd || false;
};
e("f7", "F", "id2")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Hybrid | |
Lodash | |
Native JS |
Test name | Executions per second |
---|---|
Hybrid | 17057582.0 Ops/sec |
Lodash | 5247213.0 Ops/sec |
Native JS | 73883312.0 Ops/sec |
I'll break down the benchmark definition and test cases to explain what's being tested, compared, and the pros and cons of each approach.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark on the website MeasureThat.net. The benchmark is designed to measure the performance of three different approaches: Hybrid (using Lodash), Lodash, and Native JS.
Script Preparation Code
The script preparation code provides an object fs
with multiple nested objects (f1
, f2
, ..., f7
) containing arrays of strings (A
, B
, ..., F
). This object is used to create a hierarchical data structure for testing.
Html Preparation Code
The HTML preparation code includes a link to Lodash.js, which will be used in the test cases.
Individual Test Cases
There are three test cases:
get
function to access the nested object and checks if a specific value (d
) is included in the array.includes
function to check if a specific value (d
) is included in the array.includes
function, which may not be as efficient as the Hybrid approach.d
) is included in the array.Comparison
The test cases compare the performance of these three approaches on different hardware configurations (Firefox 79 on Mac OS X 10.15). The results show that:
Pros and Cons
Best Practices
Based on these test results, it's recommended to use Native JS when:
Use Hybrid (Lodash) when:
Avoid using Lodash if:
Keep in mind that this is just a specific benchmark, and the best approach depends on the specific use case and requirements.