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

前端 未结 4 1316
囚心锁ツ
囚心锁ツ 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:24

    When it comes to performance, things can change fast. As AshleysBrain says, it comes down to whether the VM can deduce that a normal array can be implemented as a typed array quickly and accurately. That depends on the particular optimizations of the particular JavaScript VM, and it can change in any new browser version.

    This Chrome developer comment provides some guidance that worked as of June 2012:

    1. Normal arrays can be as fast as typed arrays if you do a lot of sequential access. Random access outside the bounds of the array causes the array to grow.
    2. Typed arrays are fast for access, but slow to be allocated. If you create temporary arrays frequently, avoid typed arrays. (Fixing this is possible, but it's low priority.)
    3. Micro-benchmarks such as JSPerf are not reliable for real-world performance.

    If I might elaborate on the last point, I've seen this phenomenon with Java for years. When you test the speed of a small piece of code by running it over and over again in isolation, the VM optimizes the heck out of it. It makes optimizations which only make sense for that specific test. Your benchmark can get a hundredfold speed improvement compared to running the same code inside another program, or compared to running it immediately after running several different tests that optimize the same code differently.

提交回复
热议问题