Implicit/Explicit type conversion

Benchmark created by Kyle Simpson on


Description

Want to test the effects of using variable values that must be implicitly type-coerced versus explicitly coercing them.

Preparation HTML

<script>
  var num_1 = 10;
  var num_2 = "10";
  var result_1, result_2, i;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
implicit coercion required
result_1 = "";
result_2 = 0;

for (i = 0; i < 10000; i++) {
 result_1 += num_1; // 10 will be auto-coerced to "10"
 result_2 += (num_2 - 0); // "10" will be auto-coerced to 10
}
ready
no coercion required
result_1 = 0;
result_2 = "";

for (i = 0; i < 10000; i++) {
 result_1 += num_1; // no coercion required
 result_2 += num_2; // no coercion required
}
ready
explicit coercion
result_1 = "";
result_2 = 0;

for (i = 0; i < 10000; i++) {
 result_1 += String(num_1); // 10 explicitly coerced to "10"
 result_2 += +num_2; // "10" explicitly coerced to 10
}
ready
explicit coercion #2
result_1 = "";
result_2 = 0;

for (i = 0; i < 10000; i++) {
 result_1 += String(num_1); // 10 explicitly coerced to "10"
 result_2 += ~~num_2; // "10" explicitly coerced to 10
}
ready

Revisions

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

  • Revision 1: published by Kyle Simpson on