Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
Dav Glass authored
47 lib/yui3-yui3.js
@@ -27,7 +27,8 @@ if (process.binding('natives').vm) {
27 27 useVM = require('vm');
28 28 }
29 29
30   -var coreHash = {};
  30 +var coreHash = {},
  31 + execCache = {};
31 32
32 33 var defaultConfig = {
33 34 core: '3.3.0',
@@ -102,11 +103,20 @@ exports.configure = function(c) {
102 103 exports: {}
103 104 };
104 105
  106 + if (!coreHash[core].SCRIPT) {
  107 + coreHash[core].SCRIPT = useVM.createScript(fileStr);
  108 + }
  109 +
  110 + coreHash[core].SCRIPT.runInNewContext(sandbox);
  111 + YUI = sandbox.exports.YUI;
  112 + /*
105 113 var fn = runInNewContext(fileStr, sandbox, true);
106 114 fn(sandbox.exports, require, this, __filename, __dirname);
107 115 YUI = sandbox.exports.YUI;
  116 + */
108 117
109 118 } catch (e) {
  119 + console.log(e);
110 120 throw new Error('YUI3 Core package was not found; npm install yui3-core');
111 121 return;
112 122 }
@@ -491,9 +501,12 @@ exports.configure = function(c) {
491 501 " return YUI.require(file); }; " +
492 502 data + " ;return YUI; })";
493 503
494   -
495 504 if (useVM) {
496   - fn = useVM.runInThisContext(mod, file);
  505 + //Cache the create
  506 + if (!execCache[file].script) {
  507 + execCache[file].script = useVM.createScript(mod);
  508 + }
  509 + fn = execCache[file].script.runInThisContext(mod, file);
497 510 } else {
498 511 fn = process.compile(mod, file);
499 512 }
@@ -549,12 +562,30 @@ exports.configure = function(c) {
549 562 }
550 563
551 564 } else {
552   - //Load the file locally
553   - if (YUI.loadSync) {
554   - var data = fs.readFileSync(file, encoding='utf8');
555   - loaderFn(null, data);
  565 + if (!execCache[file]) {
  566 + execCache[file] = {};
  567 + }
  568 + //console.log(file);
  569 + if (execCache[file] && execCache[file].data) {
  570 + //console.log('Loading file from execCache: ', file);
  571 + loaderFn(null, execCache[file].data);
556 572 } else {
557   - fs.readFile(file, encoding='utf8', loaderFn);
  573 + //Load the file locally
  574 + if (YUI.loadSync) {
  575 + var data = fs.readFileSync(file, encoding='utf8');
  576 + execCache[file].data = data;
  577 + loaderFn(null, data);
  578 + } else {
  579 + fs.readFile(file, encoding='utf8', (function(file) {
  580 + //console.log('READFILE: ', file);
  581 + return function(err, data) {
  582 + if (data) {
  583 + execCache[file].data = data;
  584 + }
  585 + loaderFn(err, data);
  586 + }
  587 + })(file));
  588 + }
558 589 }
559 590 }
560 591 };
4 package.json
... ... @@ -1,5 +1,5 @@
1 1 {
2   -"version":"0.5.33",
  2 +"version":"0.5.34",
3 3 "author":"Dav Glass <davglass@gmail.com>",
4 4 "bugs":{
5 5 "web":"http://github.com/davglass/nodejs-yui3/issues"},
@@ -14,7 +14,7 @@
14 14 "Standa Opichal <opichals@gmail.com>",
15 15 "Reid Burke <me@reidburke.com>"],
16 16 "engines":{
17   -"node":">=0.2.0"},
  17 +"node":">=0.4.0"},
18 18 "directories":{
19 19 "lib":"lib"},
20 20 "main":"./lib/node-yui3",
4 packages/default-package.json
... ... @@ -1,5 +1,5 @@
1 1 {
2   - "version": "0.5.33",
  2 + "version": "0.5.34",
3 3 "author": "Dav Glass <davglass@gmail.com>",
4 4 "bugs": { "web" : "http://github.com/davglass/nodejs-yui3/issues" },
5 5 "os": ["darwin", "linux"],
@@ -12,7 +12,7 @@
12 12 "Reid Burke <me@reidburke.com>"
13 13 ],
14 14 "engines": {
15   - "node" : ">=0.2.0"
  15 + "node" : ">=0.4.0"
16 16 },
17 17 "directories": {
18 18 "lib" : "lib"
44 perf/load.js
... ... @@ -0,0 +1,44 @@
  1 +#!/usr/bin/env node
  2 +
  3 +var util = require('util'),
  4 + assert = require('assert'),
  5 + i, start, end, times, startTime, endTime, t,
  6 + max = 500;
  7 +
  8 +console.log('Starting JS Load test with', max, 'requires');
  9 +
  10 +var print = function(i) {
  11 + var per = Math.round(Math.max((i / max) * 100)),
  12 + bar = '', o;
  13 + for (o = 0; o < 50; o++) {
  14 + if ((o*2) <= per) {
  15 + bar += '#';
  16 + }
  17 + }
  18 + util.print('\r(' + i + ') ' + per + '% ' + bar);
  19 +}
  20 +
  21 +startTime = (new Date()).getTime();
  22 +times = [];
  23 +
  24 +for (var i = 0; i <= max; i++) {
  25 + start = (new Date()).getTime();
  26 + var YUI = require('yui3').YUI;
  27 + var Y = YUI({debug: false}).useSync('yql');
  28 + //assert.ok(Y.YQL);
  29 + end = (new Date()).getTime();
  30 + times.push((end - start));
  31 + print(i);
  32 +}
  33 +
  34 +endTime = (new Date()).getTime();
  35 +t = 0;
  36 +
  37 +times.forEach(function(v) {
  38 + t += v;
  39 +});
  40 +
  41 +util.print('\r\n');
  42 +console.log('Test time: ', ((endTime - startTime) / 1000), 'sec');
  43 +console.log('Average Time: ', ((t / max) / 1000), 'sec');
  44 +

0 comments on commit 2866e8b

Please sign in to comment.
Something went wrong with that request. Please try again.