Are the advantages of Typed Arrays in JavaScript is that they work the same or similar in C?

前端 未结 4 1317
囚心锁ツ
囚心锁ツ 2021-01-30 17:41

I\'ve been playing around with Typed Arrays in JavaScript.

var buffer = new ArrayBuffer(16);
var int32View = new Int32Array(buffer);

I imagine

4条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-30 18:20

    I'm not really contributor to any javascript engine, only had some readings on v8, so my answer might not be completely true:

    Well values in arrays(only normal arrays with no holes/gaps, not sparse. Sparse arrays are treated as objects.) are all either pointers or a number with a fixed length(in v8 they are 32 bit, if a 31 bit integer then it's tagged with a 0 bit in the end, else it's a pointer).

    So I don't think finding the memory location is any different than a typedArray, since the number of the bytes are the same all over the array. But the difference comes that if it's an a object, then you have to add one unboxing layer, which doesn't happen for normal typedArrays.

    And ofcourse when accessing typedArrays, definitely doesn't have type checking's that a normal array have(though that might be remove in a higly optimized code, which is only generated for hot code).

    For Writing, if it's the same type shouldn't be much slower. If it's a different type then the JS engine might generate polymorphic code for it, which is slower.

    You can also try making some benchmarks on jsperf.com to confirm.

提交回复
热议问题