Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Cache the file reads and cache the script compiles so it only happens…

… once, then executes in a new context each time
  • Loading branch information...
commit 2866e8b1c960448ec6f4b29421ea283c561575eb 1 parent c15a331
@davglass authored
View
47 lib/yui3-yui3.js
@@ -27,7 +27,8 @@ if (process.binding('natives').vm) {
useVM = require('vm');
}
-var coreHash = {};
+var coreHash = {},
+ execCache = {};
var defaultConfig = {
core: '3.3.0',
@@ -102,11 +103,20 @@ exports.configure = function(c) {
exports: {}
};
+ if (!coreHash[core].SCRIPT) {
+ coreHash[core].SCRIPT = useVM.createScript(fileStr);
+ }
+
+ coreHash[core].SCRIPT.runInNewContext(sandbox);
+ YUI = sandbox.exports.YUI;
+ /*
var fn = runInNewContext(fileStr, sandbox, true);
fn(sandbox.exports, require, this, __filename, __dirname);
YUI = sandbox.exports.YUI;
+ */
} catch (e) {
+ console.log(e);
throw new Error('YUI3 Core package was not found; npm install yui3-core');
return;
}
@@ -491,9 +501,12 @@ exports.configure = function(c) {
" return YUI.require(file); }; " +
data + " ;return YUI; })";
-
if (useVM) {
- fn = useVM.runInThisContext(mod, file);
+ //Cache the create
+ if (!execCache[file].script) {
+ execCache[file].script = useVM.createScript(mod);
+ }
+ fn = execCache[file].script.runInThisContext(mod, file);
} else {
fn = process.compile(mod, file);
}
@@ -549,12 +562,30 @@ exports.configure = function(c) {
}
} else {
- //Load the file locally
- if (YUI.loadSync) {
- var data = fs.readFileSync(file, encoding='utf8');
- loaderFn(null, data);
+ if (!execCache[file]) {
+ execCache[file] = {};
+ }
+ //console.log(file);
+ if (execCache[file] && execCache[file].data) {
+ //console.log('Loading file from execCache: ', file);
+ loaderFn(null, execCache[file].data);
} else {
- fs.readFile(file, encoding='utf8', loaderFn);
+ //Load the file locally
+ if (YUI.loadSync) {
+ var data = fs.readFileSync(file, encoding='utf8');
+ execCache[file].data = data;
+ loaderFn(null, data);
+ } else {
+ fs.readFile(file, encoding='utf8', (function(file) {
+ //console.log('READFILE: ', file);
+ return function(err, data) {
+ if (data) {
+ execCache[file].data = data;
+ }
+ loaderFn(err, data);
+ }
+ })(file));
+ }
}
}
};
View
4 package.json
@@ -1,5 +1,5 @@
{
-"version":"0.5.33",
+"version":"0.5.34",
"author":"Dav Glass <davglass@gmail.com>",
"bugs":{
"web":"http://github.com/davglass/nodejs-yui3/issues"},
@@ -14,7 +14,7 @@
"Standa Opichal <opichals@gmail.com>",
"Reid Burke <me@reidburke.com>"],
"engines":{
-"node":">=0.2.0"},
+"node":">=0.4.0"},
"directories":{
"lib":"lib"},
"main":"./lib/node-yui3",
View
4 packages/default-package.json
@@ -1,5 +1,5 @@
{
- "version": "0.5.33",
+ "version": "0.5.34",
"author": "Dav Glass <davglass@gmail.com>",
"bugs": { "web" : "http://github.com/davglass/nodejs-yui3/issues" },
"os": ["darwin", "linux"],
@@ -12,7 +12,7 @@
"Reid Burke <me@reidburke.com>"
],
"engines": {
- "node" : ">=0.2.0"
+ "node" : ">=0.4.0"
},
"directories": {
"lib" : "lib"
View
44 perf/load.js
@@ -0,0 +1,44 @@
+#!/usr/bin/env node
+
+var util = require('util'),
+ assert = require('assert'),
+ i, start, end, times, startTime, endTime, t,
+ max = 500;
+
+console.log('Starting JS Load test with', max, 'requires');
+
+var print = function(i) {
+ var per = Math.round(Math.max((i / max) * 100)),
+ bar = '', o;
+ for (o = 0; o < 50; o++) {
+ if ((o*2) <= per) {
+ bar += '#';
+ }
+ }
+ util.print('\r(' + i + ') ' + per + '% ' + bar);
+}
+
+startTime = (new Date()).getTime();
+times = [];
+
+for (var i = 0; i <= max; i++) {
+ start = (new Date()).getTime();
+ var YUI = require('yui3').YUI;
+ var Y = YUI({debug: false}).useSync('yql');
+ //assert.ok(Y.YQL);
+ end = (new Date()).getTime();
+ times.push((end - start));
+ print(i);
+}
+
+endTime = (new Date()).getTime();
+t = 0;
+
+times.forEach(function(v) {
+ t += v;
+});
+
+util.print('\r\n');
+console.log('Test time: ', ((endTime - startTime) / 1000), 'sec');
+console.log('Average Time: ', ((t / max) / 1000), 'sec');
+
Please sign in to comment.
Something went wrong with that request. Please try again.