Cache in vs undefined

JavaScript performance comparison

Revision 3 of this test case created by vjeux

Preparation code

<script>
  var keyHit = 'in'
  var keyMiss = 'out'
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var cache = {}
  for (var i = 0; i < 10000; ++i) {
    cache[Math.random()] = 1
  }
  cache['in'] = 1

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Short Version - Cache hit
var res = cache[keyHit] || (cache[keyHit] = 42);
pending…
Standard way - Cache miss
var res;
if (keyMiss in cache) {
  res = cache[keyMiss];
} else {
  res = cache[keyMiss] = 42;
}
pending…
Clever Way - Cache miss
var res = cache[keyMiss];
if (res === undefined) {
  res = cache[keyMiss] = 42;
}
pending…
Clever Way - Cache hit
var res = cache[keyHit];
if (res === undefined) {
  res = cache[keyHit] = 42;
}
pending…
Standard way - Cache hit
var res;
if (keyHit in cache) {
  res = cache[keyHit];
} else {
  res = cache[keyHit] = 42;
}
pending…
Short Version - Cache miss
var res = cache[keyMiss] || (cache[keyMiss] = 42);
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.

0 Comments