JavaScript performance optimization [inline cache] V8 engine features

Yang Zhoulong 2021-02-23 15:07:49
javascript performance optimization inline cache

javascript A single thread 、 Dynamic type language , So how do we code for the best performance ? I'll talk about V8 Inline optimization of the engine . With inline caching, we can write better code .

What is inline caching

Quoting the official description : inline caching (Inline caching) It is the optimization technology adopted by the runtime system of some programming languages , The earliest is Smalltalk Development . The goal of inline caching is through Remember to go directly to the call point before To speed up method binding at run time by using the results of method queries . Inline caching is particularly useful for dynamically typed languages , Most of them ( If not all ) Method binding occurs at run time , So virtual method tables are usually not available .

  • We can understand it as javascript Every time the stack is executed, an external function is called 、 Object to generate address cache records , When the next execution reaches this location, the corresponding record will be directly retrieved from the cache , Save the process of rediscovering, from speeding up program execution .

Convert to code

Analog computing logic ( Pseudo code )
In order to better present the optimization of inline cache , We make the logic of all methods of the object consistent .
 let value = 0
const Calculator = {
add1(val) {
value += val
add2(val) {
value += val
add3(val) {
value += val
add4(val) {
value += val
add5(val) {
value += val
add6(val) {
value += val
add7(val) {
value += val
add8(val) {
value += val
add9(val) {
value += val
add10(val) {
value += val
Code that doesn't use inline optimization strategies
 function notOptimization(val, type) {
Calculator[type](val) // Dynamically executing functions , The address cannot be determined at the current point
const CalculatorKey = Object.keys(Calculator)
for(let i = 0; i < 1000000; i ++) {
const key = CalculatorKey[Math.floor(Math.random() * CalculatorKey.length)]
notOptimization(1, key)

The above code uses hash to quickly find the corresponding function , But abandon the inline caching policy . Code 5 The first execution result , Average is 80ms about
 Insert picture description here

Use inline cache policy code
 function optimization(val, type) {
if (type === 'add1')
else if (type === 'add2')
else if (type === 'add3')
else if (type === 'add4')
else if (type === 'add5')
else if (type === 'add6')
else if (type === 'add7')
else if (type === 'add8')
else if (type === 'add9')
const CalculatorKey = Object.keys(Calculator)
for(let i = 0; i < 1000000; i ++) {
const key = CalculatorKey[Math.floor(Math.random() * CalculatorKey.length)]
optimization(1, key)

This code uses multiple layers if else Make function calls ( In order to optimize the inline cache policy , It should be used correctly switch Or array lookup ), Let's look at execution 5 Secondary results , Speed is 55~50ms
 Insert picture description here

summary , So in the coding process, we try to use less dynamic call operations , But relative object property values get hashes 、 The way arrays work is much faster than if、switch Of
本文为[Yang Zhoulong]所创,转载请带上原文链接,感谢

  1. Redis solves cache, breakdown and avalanche
  2. redis
  3. Knight card: build a message center based on Kafka, and push hundreds of millions of messages easily
  4. Oracle OCP 19c 认证1Z0-083考试题库(第2题)
  5. redis的三种模式
  6. kubernetes和docker----2.学习Pod资源
  7. 谈一谈如何远程访问MySQL(腾讯云,云主机)
  8. Linux(五):Linux的文档编辑器Vi
  9. Oracle OCP 19C certification 1z0-083 examination question bank (question 2)
  10. 云原生项目实践DevOps(GitOps)+K8S+BPF+SRE,从0到1使用Golang开发生产级麻将游戏服务器—第6篇
  11. kubernetes和docker----2.学习Pod资源
  12. JSP基于Java开发Web应用程序特点有哪些?
  13. Three modes of redis
  14. Kubernetes and docker -- 2. Learning pod resources
  15. Linux (5): the document editor VI of Linux
  16. Cloud native project practice Devops (gitops) + k8s + BPF + SRE, using golang to develop production level mahjong game server from 0 to 1
  17. Kubernetes and docker -- 2. Learning pod resources
  18. What are the characteristics of JSP developing web application based on Java?
  19. Lottie error: java.lang.AssertionError : android.util.JsonReader .peek
  20. Rxhttp - lightweight, extensible, easy to use, perfectly compatible with MVVM, MVC architecture network encapsulation class library
  21. docker入门到熟练
  22. Java之HTTP网络编程(一):TCP/SSL网页下载
  23. Introduction to docker
  24. HTTP network programming in Java (1): TCP / SSL web page download
  25. mysql 的ACID以及隔离级别
  26. Acid and isolation level of MySQL
  27. Java序列化对字段名的影响
  28. The influence of Java serialization on field names
  29. Redis 日志篇:系统高可用的杀手锏
  30. Java中把一个对象复制给另外一个对象引发的思考
  31. Java之HTTP网络编程(一):TCP/SSL网页下载
  32. Redis log: the killer of system high availability
  33. Thinking about copying one object to another in Java
  34. HTTP network programming in Java (1): TCP / SSL web page download
  35. 数据库--oracle安装配置(本地安装的步骤及各种问题解决方案)
  36. 从事Java9年,27天熬夜把近年遇到的面试题收录成册全网开源!
  37. Database -- Oracle installation configuration (local installation steps and various problem solutions)
  38. Engaged in Java for 9 years, 27 days stay up late, the interview questions encountered in recent years included into a volume, the whole network open source!
  39. Java序列化 / 调用 Wildfly 服务接口异常:EJBCLIENT000409
  40. docker-compose部署Estack
  41. Redis 日志篇:系统高可用的杀手锏
  42. Java中把一个对象的值复制给另外一个对象引发的思考
  43. Java serialization / call wildfly service interface exception: ejbclient000409
  44. Docker compose deploy stack
  45. Mac下查看已安装的jdk版本及其安装目录
  46. Redis log: the killer of system high availability
  47. mybatis映射xml配置文件报错:<statement> or DELIMITER expected, got ‘id‘
  48. Thinking about copying the value of one object to another in Java
  49. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  50. vue使用sdk进行七牛云上传
  51. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  52. Spring原来还可以这么玩!阿里新产Spring全线宝典成功颠覆了我对Spring的认知!
  53. View the installed JDK version and its installation directory under mac
  54. Error in mybatis mapping XML configuration file: < statement > or delay expected, got 'ID‘
  55. IntelliJ IDEA 还能画思维导图,果然最强 IDE!
  56. Javascript性能优化【内联缓存】 V8引擎特性
  57. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  58. Vue uses SDK to upload Qi Niu cloud
  59. IntelliJ idea can also draw mind maps. It's really the strongest ide!
  60. 深入理解 Web 协议 (三):HTTP 2