b26dec implementations

JavaScript performance comparison

Test case created by Aluísio A. S. G.

Preparation code

<script>
var b26enc, b26dec_slice_reduce, b26dec_prototype_reduce, b26dec_chain, slice$ = [].slice;
b26enc = function(n){
  var chr, ref$;
  chr = String.fromCharCode;
  switch (false) {
  case !(-26 < n && n < 26):
    return chr(65 + n);
  default:
    return b26enc(Math.floor(n / 26)) + chr(65 + ((n) % (ref$ = 26) + ref$) % ref$);
  }
};
b26dec_slice_reduce = function(s){
  return slice$.call(s, 0).reduce(function(acc, cur, i){
    var v, exp;
    v = cur.charCodeAt(0) - 65;
    exp = Math.pow(26, s.length - i - 1);
    return acc + v * exp;
  }, 0);
};
b26dec_prototype_reduce = function(s){
  return Array.prototype.reduce.call(s, function(acc, cur, i){
    var v, exp;
    v = cur.charCodeAt(0) - 65;
    exp = Math.pow(26, s.length - i - 1);
    return acc + v * exp;
  }, 0);
};
b26dec_chain = function(s){
  return slice$.call(s, 0).map(function(e){
    return e.charCodeAt(0) - 65;
  }).reverse().map(function(e, i){
    return e * Math.pow(26, i);
  }).reduce(function(e, acc){
    return acc + e;
  });
};
</script>
<script>
Benchmark.prototype.setup = function() {
    var testval = b26enc(Math.floor(Math.random() * 4294967296));
};
</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
Slicing and reducing
b26dec_slice_reduce(testval);
pending…
Reducing through prototype
b26dec_prototype_reduce(testval);
pending…
Chained steps
b26dec_chain(testval);
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

0 comments

Add a comment