Preparation Code Preparation HTML (this will be inserted in the <body>
of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src ="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" >
</script >
Setup JS var i,
value,
length,
values = [],
sum = 0 ,
context = values;
for (i = 0 ; i < 10000 ; i++) {
values[i] = Math .random ();
}
function add (val ) {
sum += val;
}
function customForEach (array, callback ) {
const arrayLength = array.length
for (var i = 0 ; i < arrayLength; ++i) {
callback.call (array, array[i], i, array)
}
}
function customForEach2 (array, callback ) {
const arrayLength = array.length ;
for (var i = 0 ; i < arrayLength; ++i) {
callback (array[i]);
}
}
Teardown JS
i = 0 ;
value = 0 ;
length = 0 ;
values = [];
sum = 0 ;
Test cases
Test #1 Title *
Async
Code * values.forEach (add);
Test #2 Title *
Async
Code * for (i = 0 ; i < values.length ; i++) {
sum += values[i];
}
Title *
Async
Code * customForEach (values, add)
Title *
Async
Code * for (i = values.length - 1 ; i >= 0 ; i--) {
sum += values[i];
}
Title *
Async
Code * length = values.length ;
for (i = 0 ; i < length; i++) {
add (values[i], i, values);
}
Title *
Async
Code * length = values.length ;
for (i = 0 ; i < length; i++) {
add.call (context, values[i], i, values);
}
Title *
Async
Code * $.each (values, function (key, value ) {
sum += value;
});
Title *
Async
Code * for (i in values) {
sum += values[i];
}
Title *
Async
Code * for (i = values.length ; i--;) {
sum += values[i];
}
Title *
Async
Code * for (i = values.length - 1 ; i >= 0 ; --i) {
sum += values[i];
}
Title *
Async
Code * for (i = 0 ;
(value = values[i]) !== undefined ; i++) {
sum += value;
}
Title *
Async
Code * for (i = values.length - 1 ;
(value = values[i]) !== undefined ; i--) {
sum += value;
}
Title *
Async
Code * for (i = 0 ;
(value = values[i]) !== undefined ; i++) {
add (value, i, values);
}
Title *
Async
Code * for (i = 0 ;
(value = values[i]) !== undefined ; i++) {
add.call (context, value, i, values);
}
Title *
Async
Code * values.forEach (add);
Title *
Async
Code * values.forEach (function (val ) {
sum += val;
});
Title *
Async
Code * values.forEach (val = > sum += val);
Title *
Async
Code * sum = values.reduce ((a, b) = > a + b);
Title *
Async
Code * length = values.length ;
for (i = 0 ; i < length; i++) {
sum += values[i];
}
Title *
Async
Code * customForEach2 (values, add);
Title *
Async
Code * length = values.length ;
for (i = 0 ; i < length; i++) {
add (values[i]);
}