parseInt() vs multiply by 1 (v2)

Revision 2 of this benchmark created on


Description

Benchmarking various ways to cast a numeric string as a real INT/NUMBER.

Setup

var numeric_candidate = '10',
        nonNumeric_candidate = 'nan',
        castNumeric,
        castNonNumeric,
        castNumeric2,
        castNonNumeric2;

Test runner

Ready to run.

Testing in
TestOps/sec
parseInt() on numeric string
for (var i = 0; i <= 100000; i+=1) {
  castNumeric = parseInt(numeric_candidate, 10);
}
ready
parseInt() on non-numeric string
for (var i = 0; i <= 100000; i+=1) {
  castNonNumeric = parseInt(nonNumeric_candidate, 10);
}
ready
multiply by 1 on numeric
for (var i = 0; i <= 100000; i+=1) {
  castNumeric2 = numeric_candidate*1
}
ready
multiply by 1 on non-numeric
for (var i = 0; i <= 100000; i+=1) {
  castNonNumeric2 = nonNumeric_candidate*1
}
ready

Revisions

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