简体   繁体   English

Javascript 优化、V8 引擎和 Typescript

[英]Javascript optimization, V8 engine and Typescript

I read the following article about V8 engine and javascript optimization.我阅读了以下有关 V8 引擎和 javascript 优化的文章。

How JavaScript works: inside the V8 engine + 5 tips on how to write optimized code JavaScript 的工作原理:V8 引擎内部 + 5 个关于如何编写优化代码的技巧

According to above article, these are recommended :根据上面的文章,这些是推荐的:

a.一种。 V8 engine uses hidden classes and caching mechanism for the hidden classes and properties. V8 引擎对隐藏的类和属性使用隐藏类和缓存机制。 So avoid dynamically adding object properties, and avoid dynamically changing the type of properties.所以避免动态添加对象属性,避免动态改变属性的类型。 Instead use object constructor function.而是使用对象构造函数。

b.V8 engine applies more optimizations to frequently used functions. V8 引擎对常用功能进行了更多优化。 So avoid generating many different similar form functions executed only once.所以避免生成许多只执行一次的不同的相似表单函数。 Instead write code to repeatedly execute same function.而是编写代码以重复执行相同的功能。

Then, I have 3 questions :然后,我有3个问题:

i.一世。 Can 'a' and 'b' be applied to other browser's javascript engine, such as firefox's SpiderMonkey? 'a' 和 'b' 可以应用于其他浏览器的 javascript 引擎,例如 firefox 的 SpiderMonkey 吗?

ii. ii. According to 'a', using typescript is more advantageous for v8 engine optimization?根据'a',使用typescript对v8引擎优化更有优势? (Using typescript, you can generate javascript code with fewer dynamically changing objects and properties) (使用 typescript,您可以生成具有较少动态更改对象和属性的 javascript 代码)

iii.三、 Modern JavaScript code uses a lot of inline and anonymous functions having similar form, like the example code below.现代 JavaScript 代码使用了许多具有相似形式的内联和匿名函数,如下面的示例代码。 According to 'b', using single predefined function is more advantageous instead?根据'b',使用单个预定义函数是否更有利? (This example code is very simplified one to show similar form functions used many times) (此示例代码非常简化,以显示多次使用的类似表单功能)

prm.then((res) => {
    console.log(res);
}).then((res2) => {
    console.log(res2);
}).catch((err) => {
    console.log(err);
});

First off your title is very weak.首先你的标题很弱。 Titles should be almost in the form of a question.标题应该几乎是问题的形式。 For example: "Is it possible to optimize code for javascript engines?"例如:“是否可以优化 JavaScript 引擎的代码?”

Can 'a' and 'b' be applied to other browser's javascript engine, such as firefox's SpiderMonkey? 'a' 和 'b' 可以应用于其他浏览器的 javascript 引擎,例如 firefox 的 SpiderMonkey 吗?

Sure, but that's up to the developers of those engines to do so.当然,但这取决于这些引擎的开发人员是否这样做。 I don't think asking it here has any benefit.我不认为在这里问它有什么好处。

According to 'a', using typescript is more advantageous for v8 engine optimization?根据'a',使用typescript对v8引擎优化更有优势?

Typescript is compiled into Javascript.打字稿被编译成 Javascript。 V8 is a Javascript engine, so believing that the compiler for typescript is doing anything different for V8 OR V8 is doing any optimizations because it some how magically knows the Javascript came from Typescript, that any advantageous would be speculation at best (unless someone who is familiar with the code knows otherwise). V8 是一个 Javascript 引擎,所以相信 typescript 的编译器对 V8 做任何不同的事情,或者 V8 正在做任何优化,因为它有点神奇地知道 Javascript 来自 Typescript,任何优势充其量只是猜测(除非有人是熟悉代码的就知道否则)。

Since the TypeScript compiler is attempting to write good/optimized Javascript code anyway, V8 is simply taking advantage of good code.由于 TypeScript 编译器无论如何都在尝试编写好的/优化的 Javascript 代码,因此 V8 只是利用了好的代码。 Anyone can write good Javascript, it's just simply harder to write bad TypeScript.任何人都可以写出好的 Javascript,只是写出糟糕的 TypeScript 更难。 But it's important to note that because Typescript compiles code the same way everytime, V8 can take advantage of that, but it's only beneficial if a programmer likes writing in Typescript.但需要注意的是,因为 Typescript 每次都以相同的方式编译代码,所以 V8 可以利用这一点,但只有程序员喜欢用 Typescript 编写,这才有好处。

Modern JavaScript code uses a lot of inline and anonymous functions having similar form, like the example code below.现代 JavaScript 代码使用了许多具有相似形式的内联和匿名函数,如下面的示例代码。 According to 'b', using single predefined function is more advantageous instead?根据'b',使用单个预定义函数是否更有利?

Advantageous in what way?有什么优势?

Your anonymous functions are most likely already compiled out as console.log() is the only thing being called and could easily be in-lined.你的匿名函数很可能已经被编译出来,因为console.log()是唯一被调用的东西,可以很容易地内联。 So it's really a bad example.所以这真的是一个糟糕的例子。

As for functions that would be more complex, writing a single function is already a good principle to adhere to anyway ( Don't Repeat Yourself - DRY ).至于更复杂的函数,编写单个函数已经是一个很好的原则(不要重复自己 - DRY )。 So any programmer who keeps code DRY won't have any thing to really worry about because they've already written good code to begin with and subsequently V8 takes advantage of that.因此,任何保持代码 DRY 的程序员都不会真正担心任何事情,因为他们已经编写了良好的代码,随后 V8 就利用了这一点。

Regardless of engine, it seems that the answer really is just write good code .不管引擎如何,似乎答案真的只是写好代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM