efcl’s blog

GitHub英語のメモや引用が中心です。

JSでQueueとして配列を使った場合と自作したQueue(linked-list)での最適化の話

2 Since you're queuing functions to be executed, you can use a more efficient data structure than a JS array. A queue that has only enqueue, dequeue and isEmpty operations can have all these operations be O1. A JS array probably doesn't behave that way. See http://jsperf.com/jsqueue-vs-array

引用元: Benchmark NPO's performance against other libs · Issue #8 · getify/native-promise-only.

JavaScriptの配列でpush()shift()のみでデータを出し入れするデータ構造のケースで、シンプルにそれ用のQueueを実装するとJavaScriptエンジンの最適化が効きやすい O(1)の処理

iOSのWebViewとSafariとかで計測した結果を比べたりすると分かりやすいけど、

WebViewだと最適化全然されないので微妙な結果だけど、SafariだとQueueの方が早くなる。

後、このコードだとQueueの実装はコンストラクタ関数でやってるので、

こういう繰り返し回すベンチマークだと、ここでhidden classesみたいな最適化が効いたりしてるのがでてるのかもしれないです。

デザイン的にはJavaScriptのArrayは自由なので、目的ごとにデータ構造を作るの基本良いことだと思います。