function fillArrayWithNumbers(n) {
var arr = Array.apply(null, Array(n));
return arr.map(function (x, i) { return i });
}
var sourceArray = fillArrayWithNumbers(1000);
var targetArray = [];
for (var i = 0; i < sourceArray.length; i = i+3) {
targetArray.push(sourceArray[i]);
};
for(var i = 0; i < sourceArray.length; i++) {
sourceArray.splice(i+1,2);
}
var targetArray = sourceArray.filter(function(_, i) { return (i % 3 == 0); })
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Push to new array | |
Splice | |
Filter |
Test name | Executions per second |
---|---|
Push to new array | 13950.0 Ops/sec |
Splice | 1746399.4 Ops/sec |
Filter | 1909140.2 Ops/sec |
Let's break down the provided benchmark and explain what is being tested, compared, and the pros/cons of different approaches.
Benchmark Definition
The benchmark is defined by a JSON object that contains the following information:
Name
: The name of the benchmark.Description
: An optional description of the benchmark (not filled in this case).Script Preparation Code
: A JavaScript function that prepares the script for execution. In this case, it generates an array with 1000 elements and returns a modified version of the array that increments its index by 3 when mapped to a new value.Html Preparation Code
: An optional HTML code that prepares the page for execution (not filled in this case).Test Cases
The benchmark consists of three test cases:
sourceArray
into a new array, targetArray
. The benchmark measures how fast this operation is executed.splice()
method to remove two elements from the sourceArray
, starting from the index that would be the next element after every third element in the original array. The benchmark measures how fast this operation is executed.filter()
method to create a new array containing only the elements of the sourceArray
where the index modulo 3 equals 0 (i.e., every third element). The benchmark measures how fast this operation is executed.Options Compared
The benchmark compares three different approaches:
splice()
.Pros/Cons of Different Approaches
Here are some pros and cons of each approach:
Library Usage
The benchmark uses the Array.prototype.map()
method, which is part of the JavaScript Array prototype. This method creates a new array with the results of applying a provided function to each element in the original array.
The filter()
method is also used, which is another part of the JavaScript Array prototype. This method creates a new array containing only the elements that pass the test implemented by the provided function.
Special JS Features or Syntax
None of the benchmark cases use any special JavaScript features or syntax beyond what is part of the ECMAScript standard (e.g., let
, const
, arrow functions). However, the use of map()
, filter()
, and splice()
methods are some of the more advanced features of the language.
Other Alternatives
If you were to implement a similar benchmark for other programming languages or libraries that support array operations, you might consider using:
numpy
in Python to perform element-wise operations on arrays.Keep in mind that this is just a general guide, and the best approach will depend on the specific use case and requirements.