filter vs. handwired for loop filtering
Date tested:
3 months ago
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Test name
Executions per second
Array_filter_method
2921381.0 Ops/sec
classic_for_loop_filter
2171629.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var arr = new Array({ length: 10000 }, () => Math.floor(Math.random() * 1000));
Tests:
Array_filter_method
var isGreaterThan500 = val => val > 500; var filtered = arr.filter(isGreaterThan500)
classic_for_loop_filter
var l = arr.length var filtered = []; for (var i = 0; i < l; i++) { var val = arr[i]; if (val > 500) { filtered.push(val); } }
Open this result on MeasureThat.net