incremental variable by 1

JavaScript performance comparison

Test case created by Josh

Info

If a variable begin with a value of zero (0), what is the speed difference between adding 1 or setting the value to 1?

Preparation code

<div>
  Jumanji
</div>
<script>
Benchmark.prototype.setup = function() {
    var $res = 0;
};
</script>

Preparation code output

Jumanji

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Add 1
function addOne() {
  $res++;
}
addOne();
pending…
Set to 1
function setOne() {
  $res = 1;
}
setOne();
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