jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Fastest method to function over array
<script>
var nums = [];
for (var i = 0; i <= 1000; i++) {
nums.push(~~(Math.random()*10000));
}
function thefunc(item, array, varsObj){
varsObj.total += item;
}
var vars = {total:0}
function evalmethodset(maxsize) {
var ti = maxsize;
var x = {
thestart: function(thearr, func, vars){
var total = 0;
var i = -1;
},
theprocess: function(thearr, total, i, func, vars){
if(thearr[i++]){
func(thearr[i], thearr, vars);
}else{
return vars;
}
},
thefinish: function(vars){
return vars;
}
};
function newFilledArray(length, val) {
for (var array = [],i = 0; i < length; i++) {
array[i] = val;
}
return array;
}
var thereg = /(function\s?)([^\.])([\w|,|\s|-|_|\$]*)(.+?\{)([^\.][\s|\S]*(?=\}))/;
var thestring = '';
var theprocessstart = x.thestart.toString().match(thereg);
var theprocesscode = x.theprocess.toString().match(thereg);
var theprocessfinish = x.thefinish.toString().match(thereg);
thestring += theprocessstart[5];
var i = 0;
for(i;i<=ti;i++){
thestring += theprocesscode[5];
}
i = 0;
thestring += theprocessfinish[5];
var thefunc;
eval('thefunc = function('+theprocessstart[3]+'){'+thestring+'}');
return thefunc;
}
evalmethod = evalmethodset(1000);
function sum_recurse_set(arr, func, vars) {
var ti = arr.length - 1, i = 0, total = 0, thearr = arr, rtn;
function go(total, thearr, i,go, func, vars){
func(thearr[i], thearr, vars);
if(i !== ti){
i++;
return go(total, thearr, i,go);
}else{
return vars;
}
}
function foo(thearr, func, vars){
return go(total, thearr, i,go, func, vars);
}
return foo;
}
sum_recurse = sum_recurse_set(nums);
function sum_while(arr, func, vars) {
var total = 0,
i = 0,
len = arr.length;
while (i < len) {
func(arr[i], arr, vars)
}
return vars;
}
function sum_for(arr, func, vars) {
var total = 0,
len = arr.length;
for (var i = 0; i < len; i++) {
func(arr[i], arr, vars);
}
return vars;
}
function experiment(arr, func, vars){
var x = new Array(arr.length), i = -1, sum = 0;
while (x = arr.pop()) {
func(arr[i++], arr, vars);
}
return vars;
}
var cachedArr = new Array(nums.length);
function cachedLengthArray(arr, lenArr, func, vars){
var x = lenArr.slice(0), i = -1, sum = 0;
while (x = arr.pop()) {
func(arr[i++], arr, vars);
}
return vars;
}
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
While Loop |
| ready |
For Loop |
| ready |
eval ninja method |
| ready |
pop method destructive - removed useless |
| ready |
pop method non destructive no use for func pass removed |
| ready |
Pop method expected behaviour |
| ready |
Pop method slice cached |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.