JSFiddle - React, Tailwind, and code Playground
HTML
<div>Ordered array run took <span id='o'></span> msec.</div>
<div>Not ordered array run took <span id='no'></span> msec.</div>
CSS
body { background-color: #247; }
div { color: #fff; }
JavaScript
const size = 100000;
const timesToRepeat = 1000;
const measure = function(id, func) {
const t0 = performance.now();
for (var j = 0; j < timesToRepeat; j++) {
func();
}
const t1 = performance.now();
document.getElementById(id).innerText = Math.floor(t1 - t0);
};
const iterate = function(array) {
var sum = 0;
for (var i = 0; i < size; i++) {
if (array[i] < (size / 2)) {
sum += array[i];
}
}
return sum;
};
const notOrdered = Array.from({
length: size
}, () => Math.floor(Math.random() * size));
const ordered = Array.from({
length: size
}, (v, k) => k);
(function run() {
measure("o", function() {
iterate(ordered);
});
measure("no", function() {
iterate(notOrdered);
});
setTimeout(run, 2000);
})();