Skip to content

Commit ff814a4

Browse files
committed
making miniprofiler faster. v0.2.1
1 parent a99535b commit ff814a4

File tree

4 files changed

+42
-19
lines changed

4 files changed

+42
-19
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,20 @@ var express = require('express')
2828
, app = express();
2929

3030
app.set('view engine', 'pug');
31-
app.use(miniprofiler.profile());
31+
app.use(miniprofiler.express());
3232

3333
app.get('/', function(req, res) {
3434
req.miniprofiler.step('Step 1', function() {
3535
req.miniprofiler.step('Step 2', function() {
36-
res.render('home');
36+
res.render('index');
3737
});
3838
});
3939
});
4040

4141
app.listen(8080);
4242
```
4343

44-
`home.pug`
44+
`index.pug`
4545

4646
```javascript
4747
doctype html

lib/miniprofiler.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,12 @@
1010
* Guilherme Oenning, 2016 @goenning
1111
*/
1212
var _ = require('underscore');
13-
var fs = require('fs');
1413
var os = require('os');
15-
var path = require('path');
1614
var qs = require('querystring');
1715
var url = require('url');
1816
var uuid = require('node-uuid');
1917
var debug = require('debug')('miniprofiler');
20-
21-
_.templateSettings = {
22-
interpolate: /\{(.+?)\}/g
23-
};
18+
var ui = require('./ui.js');
2419

2520
var ignoredPaths = [];
2621
var trivialDurationThresholdMilliseconds = 2.5;
@@ -66,7 +61,6 @@ var storage = function(id, json) {
6661
return null;
6762
};
6863

69-
var includesDir = path.join(path.dirname(module.filename), '../ui');
7064
var resourcePath = '/mini-profiler-resources/';
7165
var version = '';
7266

@@ -116,7 +110,7 @@ function handleRequest(f, req, res) {
116110
}
117111

118112
function assets(req, res, lastPathSegment, done) {
119-
fs.readFile(path.join(includesDir, lastPathSegment), 'utf-8', function(err, data) {
113+
ui.readFile(lastPathSegment, function(err, data) {
120114
if (err) {
121115
debug(err);
122116
done({
@@ -163,7 +157,7 @@ function results(req, res, lastPathSegment, done) {
163157
done({
164158
type: 'text/html; charset=utf-8',
165159
status: 200,
166-
body: includes.share({
160+
body: ui.share({
167161
name: json.Name,
168162
duration: json.DurationMilliseconds,
169163
path: resourcePath,
@@ -185,13 +179,8 @@ function results(req, res, lastPathSegment, done) {
185179
});
186180
}
187181

188-
var includes = {
189-
partial: _.template(fs.readFileSync(path.join(includesDir, 'include.partial.html')).toString()),
190-
share: _.template(fs.readFileSync(path.join(includesDir, 'share.html')).toString())
191-
};
192-
193182
function include(id) {
194-
return includes.partial({
183+
return ui.partial({
195184
path: resourcePath,
196185
position: popupRenderPosition,
197186
showChildren: popupShowTimeWithChildren,

lib/ui.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
'use strict';
2+
var fs = require('fs');
3+
var path = require('path');
4+
var _ = require('underscore');
5+
6+
_.templateSettings = {
7+
interpolate: /\{(.+?)\}/g
8+
};
9+
10+
var fileStore = { };
11+
var includesDir = path.join(path.dirname(module.filename), '../ui');
12+
13+
var readFile = function(name, callback) {
14+
if (fileStore[name]) {
15+
callback(null, fileStore[name]);
16+
} else {
17+
fs.readFile(path.join(includesDir, name), 'utf-8', callback);
18+
}
19+
};
20+
21+
var templates = {
22+
partial: _.template(fs.readFileSync(path.join(includesDir, 'include.partial.html')).toString()),
23+
share: _.template(fs.readFileSync(path.join(includesDir, 'share.html')).toString())
24+
};
25+
26+
var partial = function(options) {
27+
return templates.partial(options);
28+
};
29+
30+
var share = function(options) {
31+
return templates.share(options);
32+
};
33+
34+
module.exports = { readFile, partial, share };

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "miniprofiler",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "A simple but effective mini-profiler.",
55
"main": "lib/miniprofiler.js",
66
"scripts": {

0 commit comments

Comments
 (0)