count = function(arr, item) {
return arr.filter(function(x){return x==item}).length
}
count([1,2,3,2,4,2], 2);
count = function(arr, item) {
var count = 0;
for(var i = 0; i < arr.length; ++i){
if(arr[i] == item)
count++;
}
return count
}
count([1,2,3,2,4,2], 2);
count = function(arr, item) {
return arr.reduce(function(total,x){return x==item ? total+1 : total}, 0);
}
count([1,2,3,2,4,2], 2);
count = function(arr, item) {
var regex = new RegExp('[^'+item+']+', 'g');
return String(arr).replace(regex,'').length
}
count([1,2,3,2,4,2], 3);
count = function(arr, item) {
var count= 0, i;
while((i= arr.indexOf(item, i))!= -1){
++count;
++i;
}
return count;
}
count([1,2,3,2,4,2], 2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
arr.filter | |
for | |
arr.reduce | |
regex | |
indexOf |
Test name | Executions per second |
---|---|
arr.filter | 10780288.0 Ops/sec |
for | 11988019.0 Ops/sec |
arr.reduce | 12155319.0 Ops/sec |
regex | 2858277.5 Ops/sec |
indexOf | 9523024.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Definition
The benchmark definition provides a brief description of what is being measured: counting the occurrences of an item in an array. The script preparation code and HTML preparation code are empty, indicating that these steps do not need to be executed before running the benchmarks.
Individual Test Cases
There are four test cases:
filter()
method to create a new array with only the elements that pass the test implemented by the provided function.for
loop to iterate through the array and count the occurrences of the item.reduce()
method to apply a binary function (in this case, incrementing a counter) to an accumulator value (the current element being processed) across all elements in the array.indexOf()
method to find the index of the first occurrence of the item in the array, then increments a counter for each subsequent match.Options Compared
Each test case is comparing a different approach to counting occurrences:
arr.filter
vs. for
: The filter()
method is often faster than a traditional for
loop because it avoids the overhead of incrementing a counter.arr.reduce
vs. for
: The reduce()
method can be more efficient than a traditional for
loop because it allows for accumulation and iteration in a single pass.regex
vs. indexOf
: Regular expressions are often slower than using indexOf()
because they require the browser's regex engine to execute.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Library Usage
None of the test cases use an external library (e.g., jQuery).
Special JS Features or Syntax
None of the test cases employ special JavaScript features or syntax (e.g., let
, const
, arrow functions). However, some might use modern JavaScript features not supported in older browsers.
Alternatives
Some alternative approaches to counting occurrences could be:
Array.prototype.some()
or Array.prototype.every()
.Keep in mind that each approach has its strengths and weaknesses, and the best choice depends on specific requirements and performance constraints.