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 (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
Benchmark engine:
Benchmark.js
for
7.0M/s
arr.reduce
6.3M/s
arr.filter
5.4M/s
indexOf
4.7M/s
regex
2.6M/s
View exact numbers
Test name
Executions per second
✓
for
7,019,626 Ops/sec
arr.reduce
6,324,426 Ops/sec
arr.filter
5,436,178 Ops/sec
indexOf
4,748,238 Ops/sec
regex
2,599,006 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);