Large substrings ~9000x faster in Firefox than Chrome: why?

后端 未结 2 650
我寻月下人不归
我寻月下人不归 2021-02-07 04:51

The Benchmark: http://jsperf.com/substringing

So, I\'m starting up my very first HTML5 browser-based client-side project. It\'s going to have to parse very, very large

2条回答
  •  Happy的楠姐
    2021-02-07 05:22

    In the case of Spidermonkey (the JS engine in Firefox), a substring() call just creates a new "dependent string": a string object that stores a pointer to the thing it's a substring off and the start and end offsets. This is precisely to make substring() fast, and is an obvious optimization given immutable strings.

    As for why V8 does not do that... A possibility is that V8 is trying to save space: in the dependent string setup if you hold on to the substring but forget the original string, the original string can't get GCed because the substring is using part of its string data.

    In any case, I just looked at the V8 source, ans it looks like they just don't do any sort of dependent strings at all; the comments don't explain why they don't, though.

    [Update, 12/2013]: A few months after I gave the above answer V8 added support for dependent strings, as Paul Draper points out.

提交回复
热议问题