best of string to number conversion (v42)

Revision 42 of this benchmark created on


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
*1
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = (abc[i])*1;
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
-0
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = (abc[i])-0;
  if (!isNaN(converted)) {
    result += converted
  }
}
ready
+
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = +(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.