Negativing a positive integer

JavaScript performance comparison

Test case created by Lee

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var int = 200;
};
</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
string based
var result = Number("-"+int);
 
pending…
Multiplication
var result = Number(int * -1);
 
pending…
Silly but i wondered on how bad it would be...
var result = Number(int - (int * 2));
 
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

1 comment

Lee (revision owner) commented :

Well that was surprising (for me at least). The one i thought was silly (minusing by a double of the same number) turned out to be just as quick.

Obviously the type casting in the first one is taking it's toll.

Add a comment