best of string to number conversion (v6)

Revision 6 of this benchmark created by Claudio Cicali on


Description

0| hack

Setup

var abc = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];

Test runner

Ready to run.

Testing in
TestOps/sec
Number()
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = Number(abc[i]);
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
parsetInt()
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = parseInt(abc[i], 10);
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
parseFloat()
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = parseFloat(abc[i]);
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
implicit
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = +abc[i];
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
minus 0 hack
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = abc[i]-0;
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
0| hack
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = 0|abc[i];
  if (!isNaN(converted)) {
    result += converted
  }
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.