使用Chrome开发者工具研究JavaScript里函数的原生实现
As the size of my blog Chrome Development Tool tips used in my daily work turns to be larger I create a separate post to record down this small tip.
Are you curious about the “native code” here? At least I am.
Today I find that the Profiles tab in Chrome development tool can help us to unveil the mysteries to some degree.
In Chrome development, just select this checkbox:
And then execute the simple JavaScript code below:
var arr = []; for (var i=0; i<1000; i++){ arr.push(i) } console.profile("Array toString"); for( var i = 0; i < 1000; i++){ var a = arr.toString(); } console.profileEnd("Array toString");
Once done, you can see a profile record with the name specified in JavaScript code above, “Array toString”. Hover the mouse to the first row, “anonymous function”, we find the hint “array.js”.
Switch display style from Chart to Tree:
From here the callstack of native implementation of toString is displayed:
The next step is to look into in array.js.
Launch url: https://cs.chromium.org/
Click this hyperlink:
now you can find the array.js file via path: src/v8/src/js/array.js
The callstack analyzed through the source code exactly matches the one we get in Chrome development tool Profile tab:
ArrayToString will delegate to Join if current caller is an Array:
Join will call DoJoin:
DoJoin will first call UseSparseVariant to evaluate the possibility to perform Join via SparseVariant. If not possible, call ConvertToString as fall back. ( The line number of source code may vary with the one you see in Chrome Development Tool profile tab due to the different version of Chrome being used. )
If you could not tolerate the poor performance of this online source code repository, you could download the whole source code of V8 to your local laptop by cloning this github repository:
https://chromium.googlesource.com/v8/v8.git/
本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
python语言简介及开发环境搭建的详细介绍
第一节:计算机是什么第二节:开发前的准备 第二章 python简介及环境搭建 完成了前面python开发前的准备,从这节课开始我们将会为大家介绍python语言是怎么编程的。 2.1计算机语言简介 之前的章节内容里面为大家介绍过,计算机就是一台用来计算的机器,执行人类发出的指令。但是人有人言,兽有兽语,我们需要通过计算机的语言来控制计算机。也就是编程语言。但是同学们千万不要觉得编程语言是多么高大上的东西,其实计算机语言和人类的语言没有本质的区别,不同点就是交流的主体不同!追本溯源,我们先来看一下,计算机语言的发展。计算机语言发展经历了三个阶段:机器语言机器语言是最早的语言,它是通过二进制编码来编写程序的。上节课说过二进制是计算机底层使用的进制,所以机器语言的执行效率比较高,但是缺点就是我们编写起来太麻烦。它方便了机器识别,却不方便我们人类识别。符号语言(汇编)汇编语言比机器语言又高级了一点,它是使用符号来代替机器码。编写程序时,不需要使用二进制,而是直接编写符号,编写完成后,需要将符号转换为机器码,然后再由计算机执行。符号转换为机器码的过程称为汇编,将机器码转换为符号的过程,称为反汇编...
- 下一篇
2D开发工具包揭秘:DOTween Pro入门教程
今天为大家介绍2D开发工具包中的DOTween Pro插件。DOTween是一款针对Unity的快速高效、类型安全的面向对象的补间动画引擎,并且对于C#用户做出了很多的优化。 DOTween 有哪些优秀的特性?兼顾速度与效率:DOTween不仅非常快,而且非常高效。因为一切都被缓存并重用,以避免无用的GC分配。 智能感知和类型安全所有代码都是完整的XML注释,并组织起来,以充分利用智能感知。此外,一切都是类型安全的,因为没有使用字符串。 快捷使用DOTween的调用简洁直观,如下所示:[C#] //在1秒内将transform移动到位置(1,2,3)transform.DOMove(new Vector3(1,2,3),1); //将transform的Y缩放值在1秒内变为3transform.DOScaleY(3,1); //暂停transform的补间transform.DOPause(); 非常准确:时间以非常精确的方式计算。这意味着每1000秒的1000次循环,每1000秒的循环播放完全一样长。逻辑易用的API:API旨在提高效率,直观性和易用性。一切皆可动画化:DOTween...
相关文章
文章评论
共有0条评论来说两句吧...