使用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/

本文来自云栖社区合作伙伴“汪子熙”,了解相关信息可以关注微信公众号"汪子熙"。

相关文章
|
Web App开发 JavaScript 前端开发
使用Chrome开发者工具研究JavaScript函数的原生实现原理
使用Chrome开发者工具研究JavaScript函数的原生实现原理
使用Chrome开发者工具研究JavaScript函数的原生实现原理
|
12天前
|
JavaScript 前端开发
【专栏】`Function.prototype.apply` 在JavaScript中用于动态设定函数上下文(`this`)和参数列表
【4月更文挑战第29天】`Function.prototype.apply` 在JavaScript中用于动态设定函数上下文(`this`)和参数列表。它接受两个参数:上下文对象和参数数组。理解`apply`有助于深入JS运行机制。文章分三部分探讨其原理:基本概念和用法、工作原理详解、实际应用与注意事项。在应用中要注意性能、参数类型和兼容性问题。`apply`可用于动态改变上下文、传递参数数组,甚至模拟其他语言的调用方式。通过深入理解`apply`,能提升代码质量和效率。
|
13天前
|
存储 JavaScript 前端开发
每日一道javascript面试题(九)函数的参数可以和函数体中的变量重名吗
每日一道javascript面试题(九)函数的参数可以和函数体中的变量重名吗
|
4月前
|
JavaScript 前端开发 程序员
如何像人类一样写JavaScript代码(包会)之函数参数和返回值
如何像人类一样写JavaScript代码(包会)之函数参数和返回值
119 1
|
10月前
|
JavaScript
js中函数的传递参数
js中函数的传递参数
92 0
|
10月前
|
JavaScript
js中函数的传递参数
js中函数的传递参数
|
12月前
|
前端开发 JavaScript
前端祖传三件套JavaScript的函数之函数参数
在 JavaScript 中,函数参数是非常重要的概念,它们可以帮助我们向函数中传递数据和信息,并且可以增强函数的灵活性和可维护性。本文将介绍 JavaScript 函数参数的使用方法、传递方式以及一些常见的注意事项.
55 0
|
前端开发 JavaScript
web前端-JavaScript中的函数(创建,参数,返回值,方法,函数作用域,立即执行函数)
web前端-JavaScript中的函数(创建,参数,返回值,方法,函数作用域,立即执行函数)
71 0
|
JavaScript 前端开发
【JavaScript】16_函数参数与箭头函数的参数
# 3、参数 ## 形式参数 - 在定义函数时,可以在函数中指定数量不等的形式参数(形参) - 在函数中定义形参,就相当于在函数内部声明了对应的变量但是没有赋值 ## 实际参数 - 在调用函数时,可以在函数的()传递数量不等的实参 - 实参会赋值给其对应的形参 - 参数: 1.如果实参和形参数量相同,则对应的实参赋值给对应的形参 2.如果
82 0
|
JavaScript
js基础笔记学习82-使用函数作为参数
js基础笔记学习82-使用函数作为参数
84 0
js基础笔记学习82-使用函数作为参数