best of string to number conversion (v21)

Revision 21 of this benchmark created by Mark Birbeck on


Description

Want to see if subtracting zero is just another example of 'implicit'.

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
subtract zero
var result = 0,
    converted;
for (var i = 0, len = abc.length; i < len; ++i) {
  converted = abc[i] - 0;
  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.