inlined

0.2.1 • Public • Published

inlined

throws if some module body length exceeds max_inlined_source_size number of chars

NPM version Build Status Dependency Status

js-standard-style

NPM

Installation

With npm do

npm install inlined

Description

Do you remember when NodeJS : A quick optimization advice article was published? It says that

v8 optimizer (crankshaft) inlines the functions whose body length, including the comments, is less than 600 characters.

So when you have a function or callback that’ll be called repeatedly, try to make it under 600 characters (or your tweaked value), you’ll have a quick win !

Maybe you already know that, when you require a module like this

var foo = 'bar'
 
module.exports = foo

it is wrapped in a function like this

(function (exports, require, module, __filename, __dirname) {
var foo = 'bar'
 
module.exports = foo
});

So, I guess that, and please correct me and let me know if I am wrong, if the module has less than max_inlined_source_size chars, it will be inlined hence optimized.

This package will make your code die on start up if some of your modules cannot be inlined.

Usage

Basic usage

Please note that this package is intended to be used server side. If used with browserify it is a no op.

Suppose that the main attribute in your package.json is index.js. If you want that all the modules in your package have body length less the max_inlined_source_size value, wrap your index.js this way

require('inlined')()(function () {
 
// your index.js content
 
// Every *require* call will throw an exception if its body lenght
// is greater than *max_inlined_source_size* number of chars.
})

Tuning max_inlined_source_size

Optionally, you can pass the value of the max source size which defaults to 600. It is up to you to keep it in sync with the to the max_inlined_source_size node flag. For instance, if you launch your script with

node --max_inlined_source_size=800 myscript.js

Then your code should look like

const maxInlinedSourceSize = 800
 
require('inlined')({ maxInlinedSourceSize })(function () {
 
// your index.js content
 
})

In order to do tuning on this value and change it quickly to be able to run multiple benchmarks, it also possible to set it with the MAX_INLINED_SOURCE_SIZE environment variable. For instance, if you launch your script with

export MAX_INLINED_SOURCE_SIZE=800
node --max_inlined_source_size=$MAX_INLINED_SOURCE_SIZE myscript.js

Then your code will be something like

require('inlined')()(function () {
 
// your index.js content
 
})

License

MIT

Package Sidebar

Install

npm i inlined

Weekly Downloads

15

Version

0.2.1

License

MIT

Last publish

Collaborators

  • fibo