Permalink
Browse files

micro-optimization: replacing runInNewContext with runInContext which…

… seems to be 200x faster when using a static context
  • Loading branch information...
1 parent 21075b3 commit 19a1dd56ab903939d54a2c29338f8fb6b719f51f @caridy caridy committed Dec 14, 2012
Showing with 53 additions and 42 deletions.
  1. +32 −26 lib/app/addons/rs/yui.js
  2. +21 −16 lib/management/yui-module-configurator.js
View
58 lib/app/addons/rs/yui.js
@@ -33,6 +33,20 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
yui_modules: true
},
+ // creating a vm context to execute all files
+ // we want to reuse it because it is 200x faster
+ // than creating a new one per file
+ contextForRunInContext = libvm.createContext({
+ require: require,
+ module: require('module'),
+ console: {
+ log: function() {}
+ },
+ window: {},
+ document: {},
+ YUI: null
+ }),
+
yuiSandboxFactory = require(libpath.join(__dirname, '..', '..', '..', 'yui-sandbox.js')),
syntheticStat = null,
@@ -994,37 +1008,29 @@ YUI.add('addon-rs-yui', function(Y, NAME) {
ctx,
yui = {};
file = libfs.readFileSync(res.source.fs.fullPath, 'utf8');
- ctx = {
- require: require,
- module: require('module'),
- console: {
- log: function() {}
- },
- window: {},
- document: {},
- YUI: {
- ENV: {},
- config: {},
- use: function() {},
- add: function(name, fn, version, meta) {
- yui.name = name;
- yui.version = version;
- yui.meta = meta || {};
- if (!yui.meta.requires) {
- yui.meta.requires = [];
- }
- if (runSandbox) {
- try {
- fn(runSandbox, yui.name);
- } catch (e) {
- Y.log('failed to run javascript file ' + res.source.fs.fullPath + '\n' + e.message, 'error', NAME);
- }
+ // setting up the fake YUI before executing the file
+ contextForRunInContext.YUI = {
+ ENV: {},
+ config: {},
+ use: function() {},
+ add: function(name, fn, version, meta) {
+ yui.name = name;
+ yui.version = version;
+ yui.meta = meta || {};
+ if (!yui.meta.requires) {
+ yui.meta.requires = [];
+ }
+ if (runSandbox) {
+ try {
+ fn(runSandbox, yui.name);
+ } catch (e) {
+ Y.log('failed to run javascript file ' + res.source.fs.fullPath + '\n' + e.message, 'error', NAME);
}
}
}
};
try {
- libvm.runInNewContext(file, ctx, res.source.fs.fullPath);
+ libvm.runInContext(file, contextForRunInContext, res.source.fs.fullPath);
} catch (e) {
yui = null;
Y.log('failed to parse javascript file ' + res.source.fs.fullPath + '\n' + e.message, 'error', NAME);
View
37 lib/management/yui-module-configurator.js
@@ -15,6 +15,17 @@ var fs = require('fs'),
walkDir,
updateModulesWithFile,
isExcluded,
+ // creating a vm context to execute all files
+ // we want to reuse it because it is 200x faster
+ // than creating a new one per file.
+ contextForRunInContext = vm.createContext({
+ console: {
+ log: function() {}
+ },
+ window: {},
+ document: {},
+ YUI: null
+ }),
Y = require('yui').YUI({useSync: true}).use('json-parse', 'json-stringify');
Y.applyConfig({useSync: false});
@@ -54,26 +65,20 @@ updateModulesWithFile = function(modules, meta, fullpath, excludes) {
// console.log('Configuring ' + fullpath);
file = fs.readFileSync(fullpath, 'utf8');
- ctx = {
- console: {
- log: function() {}
- },
- window: {},
- document: {},
- YUI: {
- add: function(name, fn, version, meta) {
- if (!meta) {
- meta = {};
- }
- modules[name] = {
- fullpath: fullpath,
- requires: meta.requires || []
- };
+ // setting up the fake YUI before executing the file
+ contextForRunInContext.YUI = {
+ add: function(name, fn, version, meta) {
+ if (!meta) {
+ meta = {};
}
+ modules[name] = {
+ fullpath: fullpath,
+ requires: meta.requires || []
+ };
}
};
try {
- vm.runInNewContext(file, ctx, fullpath);
+ vm.runInContext(file, contextForRunInContext, fullpath);
} catch (e) {
utils.error(e.message + ' in file: ' + fullpath, null, true);
}

0 comments on commit 19a1dd5

Please sign in to comment.