numers as text and binary

JavaScript performance comparison

Test case created by Niklas Werner


IMPORTANT: for easier comparison of the bars in the chart, tests 3 and 4 (binary tests) are executed 10 times so they are actually much faster as it looks like in the chart

NOTE: Binary operations may seem to be much faster but in fact creating ArrayBuffers and appropriate views takes much more time (see "slow" snippets). Additionally TypedArrays always use host-endianness whereas DataView can handle both big endian and little endian on both types of machines. But - there's always a 'but' - ArrayBuffer is much slower than TypedArrays (see DataView vs. TypedArrayViews).

Preparation code

var integerNumber = 1234567890;
var floatingNumber = 1234567890e-6;
var integerString = String(integerNumber);
var floatingString = String(floatingNumber);
var integerBytes = new ArrayBuffer(4);
var integerBytesView = new Int32Array(integerBytes);
integerBytesView[0] = integerNumber;
var floatingBytes = new ArrayBuffer(4);
var floatingBytesView = new Float32Array(floatingBytes);
floatingBytesView[0] = floatingNumber;
Benchmark.prototype.setup = function() {
    var buffer = new ArrayBuffer(4);
    var integerBytesView2 = new Int32Array(buffer);
    var floatingBytesView2 = new Float32Array(buffer);

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
to string
var si = String(integerNumber);
var sf = String(floatingNumber);
from string
var i = parseInt(integerString);
var f = parseFloat(floatingString);
to bytes
for(var _l = 0; _l < 10; _l++) {
  integerBytesView2[0] = integerNumber;
  floatingBytesView2[0] = floatingNumber;
from bytes
for(var _l = 0; _l < 10; _l++) {
  var i = integerBytesView[0];
  var f = floatingBytesView[0];
to bytes (slow)
new Int32Array(buffer)[0] = integerNumber;
new Float32Array(buffer)[0] = floatingNumber;
from bytes (slow)
var i = new Int32Array(integerBytes)[0];
var f = new Float32Array(floatingBytes)[0];

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

Compare results of other browsers


Comment form temporarily disabled.

Add a comment