float vs int addition

JavaScript performance comparison

Revision 3 of this test case created by ckknight

Info

Test if javascript engines can optimize unsigned integer addition.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var ints = [11, 22, 33, 44, 55, 66, 77, 88, 99, 1010];
    var floats = [1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10];
    var sparse = [1.1, , 3.3, , 5.5, , 7.7, , 9.9, , ]
};
</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
ints
var sum = 0;
for (var i = 0; i < ints.length; i++) {
  sum += ints[i];
}
pending…
floats
var sum = 0;
for (var i = 0; i < floats.length; i++) {
  sum += floats[i];
}
pending…
casted ints
var sum = 0;
for (var i = 0; i < floats.length; i++) {
  sum += floats[i] | 0;
}
pending…
casted ints
var sum = 0;
for (var i = 0; i < ints.length; i++) {
  sum += ints[i]|0;
}
pending…
ints, cast on assign
var sum = 0;
for (var i = 0; i < ints.length; i++) {
  sum = (sum + ints[i])|0;
}
pending…
ints, always cast
var sum = 0;
for (var i = 0; i < ints.length; i++) {
  sum = ((sum|0) + (ints[i]|0))|0;
}
pending…
floats, cast on assign
var sum = 0;
for (var i = 0; i < floats.length; i++) {
  sum = (sum + floats[i]) | 0;
}
pending…
floats, always cast
var sum = 0;
for (var i = 0; i < floats.length; i++) {
  sum = ((sum|0) + (floats[i]|0)) | 0;
}
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