Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
Run results for:
js Count the occurences of an item in an array
Go to the benchmark
Embed
Embed Benchmark Result
Run details:
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser:
Chrome 131
Operating system:
Mac OS X 10.15.7
Device Platform:
Desktop
Date tested:
one year ago
Benchmark engine:
Benchmark.js
arr.reduce
12.2M/s
for
12.0M/s
arr.filter
10.8M/s
indexOf
9.5M/s
regex
2.9M/s
View exact numbers
Test name
Executions per second
✓
arr.reduce
12,155,319 Ops/sec
for
11,988,019 Ops/sec
arr.filter
10,780,288 Ops/sec
indexOf
9,523,024 Ops/sec
regex
2,858,278 Ops/sec
Tests:
arr.filter
count = function(arr, item) { return arr.filter(function(x){return x==item}).length } count([1,2,3,2,4,2], 2);
for
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);
arr.reduce
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);
regex
count = function(arr, item) { var regex = new RegExp('[^'+item+']+', 'g'); return String(arr).replace(regex,'').length } count([1,2,3,2,4,2], 3);
indexOf
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);