Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
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
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
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);