Javascript子串是虚拟的吗? [英] Is Javascript substring virtual?

查看:178
本文介绍了Javascript子串是虚拟的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有一个巨大的字符串,名为 str1 ,比如500万字符长,然后 str2 = str1.substr(5555,100) 以便 str2 长度为100个字符,并且是 str1 的子字符串,从5555开始(或者任何其他随机选择的位置。)

If we have a huge string, named str1, say 5 million characters long, and then str2 = str1.substr(5555, 100) so that str2 is 100 characters long and is a substring of str1 starting at 5555 (or any other randomly selected position).

JavaScript如何在内部存储 str2 ?是复制字符串内容还是新字符串是虚拟的,只存储对原始字符串的引用以及位置和大小的值?

How JavaScript stores str2 internally? Is the string contents copied or the new string is sort of virtual and only a reference to the original string and values for position and size are stored?

我知道这是实现依赖,ECMAScript标准(可能)没有定义字符串实现的内幕。但我想知道一些从内部了解V8或SpiderMonkey的专家,以便澄清这一点。

I know this is implementation dependent, ECMAScript standard (probably) does not define what's under the hood of the string implementation. But I want to know from some expert who knows V8 or SpiderMonkey from inside well enough to clarify this.

谢谢

推荐答案

AFAIK V8有四种字符串表示形式:

AFAIK V8 has four string representations:


  1. ASCII

  2. UTF-16

  3. 多个字符串的连接

  4. 另一个字符串的片段

  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做同样的事情。 (参见 Firefox中的大子串~9000x比Chrome快:为什么? ...虽然Chrome的答案已经过时了。)

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

这可以提供真正的速度提升,但有时这是不可取的,因为它可能导致小字符串保留较大父字符串的内存( V8错误报告

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)

这篇关于Javascript子串是虚拟的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆