Is Javascript substring virtual?

给你一囗甜甜゛ 提交于 2019-11-29 01:31:44
Paul Draper

AFAIK V8 has four string representations:

  1. ASCII
  2. UTF-16
  3. concatenation of multiple strings
  4. slice of another string

Thus, it does not have to copy the string; it just has to beginning and ending markers to the other string.

SpiderMonkey does the same thing. (See Large substrings ~9000x faster in Firefox than Chrome: why? ... though the answer for Chrome is outdated.)

This can give real speed boosts, but sometimes this is undesirable, since it can cause small strings to hold onto the memory of the larger parent string (V8 bug report)

This old blog post of mine explains it, as well as some other string representation forms: http://blog.cdleary.com/2012/01/string-representation-in-spidermonkey/

Search for "dependent string". I think I know what you might be getting at with the question: they can be problematic things, at times, because if there are no references to the original, you can keep a giant string around in order to keep a bitty little substring that's actually semantically reachable. There are things that an implementation could do to mitigate that problem, like record information on a GC-generation basis to see if such one-dependent-string entities exist and collapse them to their minimal size, but last I knew of that was not being done. (Essentially with that kind of approach you're recovering runtime_refcount == 1 style information at GC-sweep time.)

candu

Strings are immutable, and any operations on them create new strings. str2 is an entirely new string, containing data copied from str1.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!