javascript benchmark

js:


function generateRandomArray(size, maxElement) {

    var array = [];

    for (var i = 0; i < size; i += 1) {

        array.push(Math.floor(Math.random() * (maxElement + 1)));

    }

    return array;

}



var new_big_array = generateRandomArray(1000000, 100); // generate array with 1 million elements



function sortArray(input_arr) {

    var startTimeStamp = new Date().getTime(),

        duration;



    input_arr.sort(function (a, b) { // sort array

        return a - b;

    });



    duration = new Date().getTime() - startTimeStamp; // javascript benchmark - difference between start and end time in milliseconds

    //alert('Sorting of array with 1 million elements lasts ' + duration + ' milliseconds;');

    document.getElementById('log').innerHTML = 'Sorting of array with 1 million elements lasts ' + duration + ' milliseconds;';

}



sortArray(new_big_array);

html:


<div id="log"></div>

Leave a Comment