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 (iPhone; CPU iPhone OS 16_6_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Mobile/15E148 Safari/604.1
Browser:
Mobile Safari 16
Operating system:
iOS 16.6.1
Device Platform:
Mobile
Date tested:
2 years ago
Test name
Executions per second
arr.filter
5436177.5 Ops/sec
for
7019625.5 Ops/sec
arr.reduce
6324426.0 Ops/sec
regex
2599006.0 Ops/sec
indexOf
4748238.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);