V8常见去优化原因一览

简介:

原作者:江凌

V8 bailout reasons

v8 bailout reasons 的例子, 解释和建议. 帮助alinode的用户根据 CPU-Profiler 的提示进行优化。

索引

Bailout reasons

Bailout reasons

Assignment to parameter in arguments object

  • 简单例子
// sloppy mode only
function test(a) {
  if (arguments.length < 2) {
    a = 0;
  }
}
  • Why

    • 只会在函数中重新赋值参数发生。
  • Advices

    • 你不能给变量 a 重新赋值.
    • 最好使用 strict mode .
    • V8 最新的 TurboFan 会有优化 #1.

Bad value context for arguments value

  • 简单例子
// strict & sloppy modes
function test1() {
  arguments[0] = 0;
}

// strict & sloppy modes
function test2() {
  arguments.length = 0;
}

// strict & sloppy modes
function test3() {
  return arguments;
}

// strict & sloppy modes
function test4() {
  var args = [].slice.call(arguments);
}

// strict & sloppy modes
function test5() {
  var a = arguments;
  return function() {
    return a;
  };
}

ForInStatement with non-local each variable

  • 简单例子
// strict & sloppy modes
function test1() {
  var obj = {};
  for(key in obj);
}

// strict & sloppy modes
function key() {
  return 'a';
}
function test2() {
  var obj = {};
  for(key() in obj);
}

Object literal with complex property

  • 简单例子
// strict & sloppy modes
function test() {
  return {
    __proto__: 3
  };
}
  • Why
  • Advices

    • 简化 Object。

ForInStatement is not fast case

  • 简单例子
for (var prop in obj) {
  /* lots of code */
}
  • Why

    • for 循环中包含太多的代码。
  • Advices

    • for 循环中的提取代码提取为函数。

Reference to a variable which requires dynamic lookup

  • 简单例子
// sloppy mode only
function test() {
  with ({x:1}) {
    return x;
  }
}
  • Why

    • 编译时编译定位失败,Crankshaft需要重新动态查找。#3
  • Advices

    • TurboFan可以优化。

TryCatchStatement

  • 简单例子
// strict & sloppy modes OR // sloppy mode only
function func() {
  return 3;
  try {} catch(e) {}
}
  • Why

    • try/catch 使得控制流不稳定,很难在运行时优化。
  • Advices

    • 不要在负载重的函数中使用try/catch.
    • 可以重构为 try { func() } catch

TryFinallyStatement

  • 简单例子
// strict & sloppy modes OR // sloppy mode only
function func() {
  return 3;
  try {} finally {}
}

Unsupported phi use of arguments

  • 简单例子
// strict & sloppy modes
function test1() {
  var _arguments = arguments;
  if (0 === 0) { // anything evaluating to true, except a number or `true`
    _arguments = [0]; // Unsupported phi use of arguments
  }
}

// strict & sloppy modes
function test2() {
  var _arguments = arguments;
  for (var i = 0; i < 1; i++) {
    _arguments = [0]; // Unsupported phi use of arguments
  }
}

// strict & sloppy modes
function test3() {
  var _arguments = arguments;
  var again = true;
  while (again) {
    _arguments = [0]; // Unsupported phi use of arguments
    again = false;
  }
}
  • Why

    • Crankshaft 无法知道 _arguments是 object 或 array.
    • 深入了解
  • Advices

    • 最好操作 arguments 的拷贝.
    • TurboFan 可以优化 #1.

Yield

  • 简单例子
// strict & sloppy modes
function* test() {
  yield 0;
}

Resources

目录
相关文章
|
SQL 关系型数据库 MySQL
MySQL唯一约束(UNIQUE KEY)
MySQL唯一约束(UNIQUE KEY)
730 0
如何对BigDecimal进行非0判断
如何对BigDecimal进行非0判断
377 3
|
人工智能 自然语言处理 测试技术
Claude 3非常厉害,但是国内用不上怎么办?
【2月更文挑战第16天】Claude 3非常厉害,但是国内用不上怎么办?
4681 1
Claude 3非常厉害,但是国内用不上怎么办?
|
SQL 安全 IDE
SonarQube使用介绍
SonarQube使用介绍
3114 0
SonarQube使用介绍
|
Java Maven 索引
Logback:同时按照日期和大小分割日志(最新日志可以不带日期或数字)
Logback:同时按照日期和大小分割日志(最新日志可以不带日期或数字)
Logback:同时按照日期和大小分割日志(最新日志可以不带日期或数字)
|
11月前
|
传感器 存储 数据采集
深入调查研究GE-Predix
【11月更文挑战第8天】
1196 2
|
11月前
|
API 数据安全/隐私保护 开发者
实时获取小红书详情 API 数据
小红书详情API数据获取指南:注册开发者账号,创建应用并申请接口权限,构建请求获取笔记详情,使用Python等语言处理响应数据。需遵守使用规则,注意调用频率和数据安全。
|
12月前
|
JavaScript 前端开发 编译器
掌握现代化JavaScript:ECMAScript提案与特性
【10月更文挑战第13天】本文介绍了ECMAScript(ES)的最新提案与特性,包括可选链、空值合并运算符、类字段和顶层Await等。通过跟踪TC39提案、使用Babel或TypeScript、测试兼容性以及逐步迁移,开发者可以高效地采用这些新特性,简化代码、提高开发效率并增强应用功能。文章还提供了实战技巧,帮助开发者在现代Web开发中充分利用这些现代化的特性。
|
11月前
|
人工智能 安全 Java
关于Claude国内能用吗?回答是:能用!
想体验 Anthropic 公司打造的 AI 助手 Claude 的强大功能,却受限于网络?别担心!这篇指南将为你揭开 Claude 的神秘面纱,并提供多种途径,助你轻松跨越网络障碍,开启 AI 新世界!🎉
|
Java 数据库连接 数据格式
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit
IOC/DI配置管理DruidDataSource和properties、核心容器的创建、获取bean的方式、spring注解开发、注解开发管理第三方bean、Spring整合Mybatis和Junit
【Java笔记+踩坑】Spring基础2——IOC,DI注解开发、整合Mybatis,Junit