Comparison of numbers

JavaScript performance comparison

Test case created by Rob W

Info

See: <a target="_blank" href="http://stackoverflow.com/questions/8864430/compare-javascript-array-of-objects-to-get-min-max">http://stackoverflow.com/questions/8864430/compare-javascript-array-of-objects-to-get-min-max</a>

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var myArray =
    [
        {"ID": 1, "Cost": 200},
        {"ID": 2, "Cost": 1000},
        {"ID": 3, "Cost": 50},
        {"ID": 4, "Cost": 500}
    ]
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Rob W
var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
    tmp = myArray[i].Cost;
    if (tmp < lowest) lowest = tmp;
    if (tmp > highest) highest = tmp;
}
pending…
32bitkid
function finder(cmp, arr, attr) {
    var val = arr[0][attr];
    for(var i=1;i<arr.length;i++) {
        val = cmp(val, arr[i][attr]);
    }
    return val;
}

var highest = finder(Math.max, myArray, "Cost");
var lowest = finder(Math.min, myArray, "Cost");
 
pending…
32bitkid, optimized
var highest = myArray[0].Cost;
var lowest = myArray[0].Cost;
for(var i=1;i<myArray.length;i++) {
    highest = Math.max(highest, myArray[i].Cost);
    lowest = Math.min(lowest, myArray[i].Cost);
}
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment