Math.max() vs Math.max.apply()

JavaScript performance comparison

Test case created by maimairel

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

<div data-x="1" data-y="1"></div>
<div data-x="2" data-y="1"></div>
<div data-x="3" data-y="1"></div>
<div data-x="4" data-y="1"></div>
<div data-x="5" data-y="1"></div>
<div data-x="1" data-y="2"></div>
<div data-x="2" data-y="2"></div>
<div data-x="3" data-y="2"></div>
<div data-x="4" data-y="2"></div>
<div data-x="5" data-y="2"></div>
<div data-x="1" data-y="3"></div>
<div data-x="2" data-y="3"></div>
<div data-x="3" data-y="3"></div>
<div data-x="4" data-y="3"></div>
<div data-x="5" data-y="3"></div>
 

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Math.max
var maxX = 0, maxY = 0;
$( 'div' ).each(function( index, div ) {
  maxX = Math.max( maxX, $( div ).data( 'x' ) );
  maxY = Math.max( maxY, $( div ).data( 'y' ) );
});
pending…
Math.max.apply
var xa = [], ya = [];
var maxX = 0, maxY = 0;

$( 'div' ).each(function( index, div ) {
        xa.push( $( div ).data( 'x' ) );
        ya.push( $( div ).data( 'y' ) );
});
maxX = Math.max.apply( Math, xa );
maxY = Math.max.apply( Math, ya );
pending…
Math.max with object cache
var maxX = 0, maxY = 0, d;
$( 'div' ).each(function( index, div ) {
  d = $( div ).data();
  maxX = Math.max( maxX, d.x );
  maxY = Math.max( maxY, d.y );
});
pending…
Math.max.apply with object cache
var xa = [], ya = [], d;
var maxX = 0, maxY = 0;

$( 'div' ).each(function( index, div ) {
  d = $( div ).data();
        xa.push( d.x );
        ya.push( d.y );
});
maxX = Math.max.apply( Math, xa );
maxY = Math.max.apply( Math, ya );
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

Compare results of other browsers

0 comments

Add a comment