parseInt vs Math.floor vs bitwise operator vs bitwise operator

JavaScript performance comparison

Revision 2 of this test case created by Christian Dannie Storgaard

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var numbers = [87.57421753834933, 26.26186180859804, 71.62071967031807, 65.87039527948946, 41.01329643744975, 74.29505824111402, 99.0098969778046, 1.2578653171658516, 84.19486486818641, 49.89710508380085, 38.536189519800246, 53.994893468916416, 65.00874462071806, 75.5798309808597, 64.64001117274165, 61.94389455486089, 11.629014974460006, 81.05128067545593, 87.19723785761744, 16.800769860856235, 32.70094443578273, 43.43707801308483, 56.214373745024204, 28.27581411693245, 81.57095320057124, 85.79192864708602, 2.4131825426593423, 78.0902827391401, 27.65540392138064, 18.62477830145508]
    var number = 1
    var i = 0
};
</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
parseInt
i = 0;
while (i < numbers.length) {
  number = parseInt(numbers[i]);
  ++i
}
pending…
Math.floor
i = 0;
while (i < numbers.length) {
  number = Math.floor(numbers[i]);
  ++i
}
pending…
~~number
i = 0;
while (i < numbers.length) {
  number = ~~numbers[i];
  ++i
}
pending…
number | 0
i = 0;
while (i < numbers.length) {
  number = numbers[i] | 0;
  ++i
}
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