I noticed that TypedArrays use `calloc`, when not backed by a Buffer, which 
seemed like something that'd be slow. I sat down and wrote a quick JS 
implementation, that relies only on Buffer and a slightly modified 
`SlowBuffer.makeFastBuffer`. It's a stand-alone package at:
  https://github.com/stephank/node-arrays

It requires Node 0.7.7, which includes Buffer alignment fixes. It passes the 
WebGL conformance tests. I made a quick benchmark to compare performance with 
the native implementation currently in Node. On the two Macs I have access to, 
I get about the same numbers:

    $ node test/benchmark.js
    Small ArrayBuffer: 0.00149 ms (native) vs. 0.000603 ms (pure-js), ×0.40
    Large ArrayBuffer: 0.007311 ms (native) vs. 0.008317 ms (pure-js), ×1.14
    Small Int8Array: 0.001472 ms (native) vs. 0.00066 ms (pure-js), ×0.45
    Large Int8Array: 0.007359 ms (native) vs. 0.008385 ms (pure-js), ×1.14



For small allocations, there's a clear win from Buffer's pooling. There's a 
slight loss for allocations exceeding the pool size, but then again, I've not 
yet profiled the code.

If anyone feels the need to try this on a real world project, there's a node 
branch at:
  https://github.com/stephank/node/tree/js-arrays

Note that there are slight differences in behavior for stuff that falls outside 
the spec; see:
  https://github.com/stephank/node/commit/3c13517a

--  
Stéphan Kochen


Reply via email to