javascript code speed test code example

Example 1: test load function benchmark javascript

var iterations = 1000000;
console.time('Function #1');
for(var i = 0; i < iterations; i++ ){
    functionOne();
};
console.timeEnd('Function #1')

console.time('Function #2');
for(var i = 0; i < iterations; i++ ){
    functionTwo();
};
console.timeEnd('Function #2')

Example 2: javascript code speed test

function quickSortMain2(array) {
    console.log("array", array);
    var l = array.length;
    if(l <= 1) return array;
    const pivot = array[l-1];
    const leftArr = [];
    const rightArr = [];
    for (const el of array.slice(0, l-1)) el < pivot ? leftArr.push(el) : rightArr.push(el);
    if(leftArr.length > 0 && rightArr.length > 0) return [...quickSortMain2(leftArr), pivot, ...quickSortMain2(rightArr)];
    else if(leftArr.length) return [...quickSortMain2(leftArr), pivot];
    else if(rightArr.length > 0) return [pivot, ...quickSortMain2(rightArr)];
}
var items = [5,3,7,6,2,9];
var sortedArray = quickSortMain2(items);
console.log(sortedArray);

Tags:

Misc Example