In-depth understanding of Node.js
  • README
  • Introduction to Node.js
    • Overview of the architecture
    • Why libuv?
    • V8 concepts
    • C++ and JS interaction
  • Starting with hello world
  • Module loading
  • Global object
  • Event loop
  • Timer interpretation
  • Yield magic
  • Buffer
  • Event
  • Domain
  • Stream
  • Net
    • Socket
    • Building applications
    • Encryption
  • HTTP
    • HTTP Server
    • HTTP Client
  • FS file system
    • File system
    • File abstraction
    • IO
    • libuv selection
    • File IO
    • Fs essence
  • Process
    • Process
    • Cluster
  • Node.js pitfalls
  • Others
    • Node.js & Android
    • Node.js & Docker
    • Node.js tuning
  • Appendix
Powered by GitBook
On this page
  • Index
  • Bailout reasons
  • Bailout reasons
  • Assignment to parameter in arguments object
  • Bad value context for arguments value
  • ForInStatement with non-local each variable
  • Object literal with complex property
  • ForInStatement is not fast case
  • Reference to a variable which requires dynamic lookup
  • TryCatchStatement
  • TryFinallyStatement
  • Unsupported phi use of arguments
  • Resources

Appendix

PreviousNode.js tuning

Last updated 2 years ago

Examples, explanations and suggestions for v8 bailout reasons. Help alinode users optimize according to CPU-Profiler prompts.

Index

Bailout reasons

Assignment to parameter in arguments object

  • Simple example

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

    • Only occurs when reassigning parameters in a function.

  • Advices

    • You cannot reassign variable a.

    • It is better to use strict mode.

Bad value context for arguments value

  • Simple example

// 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;
  };
}
  • Why

    • Requires further specification of the arguments array.

  • Advices

    • You can read: https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#3-managing-arguments

  • External links

    • https://github.com/bevry/taskgroup/issues/12

ForInStatement with non-local each variable

  • Simple example

// 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);
}
  • Why

    • https://github.com/yjhjstz/v8-git-mirror/blob/master/src/hydrogen.cc#L5254

  • Advices

    • Only pure local variables can be used in for...in.

    • https://github.com/petkaantonov/bluebird/wiki/Optimization-killers#5-for-in

  • External links

    • https://github.com/mbostock/d3/pull/2686

Object literal with complex property

  • Simple example

// strict & sloppy modes
function test() {
  return {
    __proto__: 3
  };
}
  • Why

  • Advices

    • Simplify the Object.

ForInStatement is not fast case

  • Simple example

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

    • Too much code in the for loop.

  • Advices

    • Extract the code in the for loop into a function.

Reference to a variable which requires dynamic lookup

  • Simple example

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

  • Advices

    • TurboFan can optimize.

TryCatchStatement

  • Simple example

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

    • try/catch makes the control flow unstable and difficult to optimize at runtime.

  • Advices

    • Do not use try/catch in heavily loaded functions.

    • Can be refactored as try { func() } catch

TryFinallyStatement

  • Simple example

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

  • Advices

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 cannot determine whether _arguments is an object or an array.

  • Advices

    • It is best to operate on a copy of arguments.




### Yield

* Simple example

```js
// strict & sloppy modes
function* test() {
  yield 0;
}
  • Why

    • Generator state preservation and restoration is achieved by copying function stack frames, which is not suitable for optimized compilers.

  • Advices

    • Currently, there is no need to consider this issue. TurboFan can optimize.

  • External links:

    • https://groups.google.com/forum/#!topic/v8-users/KnnUb-u4rA8


Resources

V8's latest TurboFan will have optimizations .

You can loop through arguments to create a new array

V8's latest TurboFan will have optimizations .

Compilation fails to locate the variable at compile time, and Crankshaft needs to dynamically look it up again.

See

See

TurboFan can optimize .

#1
#1
More
#3
Learn more
#1
All bailout reasons in Chromium codebase
Bad value context for arguments value
I-want-to-optimize-my-JS-application-on-V8 checklist
JavaScript: Performance loss on incorrect arguments using
Optimization killers
OptimizationKillers
Performance Tips for JavaScript in V8
thlorenz/v8-perf
Bailout reasons
Assignment to parameter in arguments object
Bad value context for arguments value
ForInStatement with non-local each variable
Object literal with complex property
ForInStatement is not fast case
Reference to a variable which requires dynamic lookup
TryCatchStatement
TryFinallyStatement
Unsupported phi use of arguments
Yield
Unsupported phi use of arguments
TryCatchStatement
TryCatchStatement