<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
var max1 = 1000; // 100,000 (100 Thousand)
var max2 = 10000000; // 10,000,000 (10 Million)
var max3 = 100000000; // 100,000,000 (100 Million)
var arr1 = [];
for (var i = 0; i <= max1; i++) { arr1.push(i); }
var arr2 = [];
// for (var i = 0; i <= max2; i++) { arr2.push(i); }
var arr3 = [];
//for (var i = 0; i <= max3; i++) { arr3.push(i); }
arr1.map(function (element, index) {
element = element*2;
});
_.map(arr1, function (element, index) {
element = element*2;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Native | |
Lodash.js filter |
Test name | Executions per second |
---|---|
Native | 473572.2 Ops/sec |
Lodash.js filter | 493951.0 Ops/sec |
I'll break down the provided benchmark and its explanations into understandable parts.
Benchmark Definition JSON
The provided benchmark definition consists of two main parts: Script Preparation Code
and Html Preparation Code
. The latter includes a script tag referencing the Lodash.js library, which is used in one of the test cases.
Script Preparation Code
This section creates three arrays (arr1
, arr2
, and arr3
) with increasing size (1000, 10,000,000, and 100,000,000 elements respectively) using a for loop. The purpose of creating these large arrays is to simulate a scenario where the browser needs to map over a large dataset.
Html Preparation Code
This section includes a script tag that loads the Lodash.js library version 4.17.4 from a CDN.
Test Cases
There are two individual test cases:
map()
function without any additional libraries._map()
function, which is similar to the native map()
function but provides additional features and optimizations.Options Compared
The two test cases compare:
map()
function vs Lodash.js' _map()
functionPros and Cons
Native JavaScript's map()
function:
Pros:
Cons:
Lodash.js' _map()
function:
Pros:
map()
function in some casesCons:
Other Considerations
Library: Lodash.js
Lodash.js is a popular utility library for JavaScript. Its _map()
function provides a convenient and efficient way to map over arrays. In this benchmark, it's used to compare the performance of native JavaScript's map()
function against its own implementation.
If you're interested in exploring alternative approaches, here are some options:
map()
function.map()
function. Examples include Array.prototype.map() from the ECMAScript specification, as well as specialized libraries like FastArray and ArrayProto.map()
function and Lodash.js' _map()
function.