Skip to content

Commit 7a6193f

Browse files
committed
fix benchmark.js and readme
1 parent 3c891d3 commit 7a6193f

File tree

2 files changed

+49
-27
lines changed

2 files changed

+49
-27
lines changed

README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ SYNOPSIS
4646
```
4747
// foo.js
4848
window.onload = function () {
49-
var html = template('tmpl1', { isFoo : true,
49+
var html = template('tmpl1', { isFoo : true,
5050
foobar : 'foobar!!',
5151
foobaz : 'foobaz!!',
5252
html : '<marquee>Helloooo</marquee>'
@@ -162,14 +162,29 @@ template('view1', { foo : 'bar' });
162162
BENCHMARK
163163
=========
164164

165-
on Browsers:
166-
167-
* http://jsdo.it/cho45/rjwe/fullscreen
168-
169165
node:
170166

171167
* node misc/benchmark.js
172168

169+
```
170+
A larger number (count) means faster. A smaller number (msec) means faster.
171+
Linux (linux) x64 6.6.87.1-microsoft-standard-WSL2 13th Gen Intel(R) Core(TM) i7-13700K 24 cpus
172+
running... micro-template
173+
running... micro-template (escaped)
174+
running... micro-template (without `with`)
175+
running... John Resig's tmpl
176+
running... ejs.render
177+
running... ejs.render pre compiled
178+
=== result ===
179+
52736.3: (0.019 msec) micro-template (without `with`)
180+
6226.1: (0.161 msec) John Resig's tmpl
181+
4771: (0.21 msec) micro-template
182+
4424.8: (0.226 msec) micro-template (escaped)
183+
4322.8: (0.231 msec) ejs.render pre compiled
184+
3853.6: (0.26 msec) ejs.render
185+
node misc/benchmark.js 7.41s user 0.01s system 101% cpu 7.349 total
186+
```
187+
173188

174189
LICENSE
175190
=======

misc/benchmark.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#!/usr/bin/env node
22

33
// ============================================================================
4-
var template = require('../lib/micro-template.js').template;
4+
import assert from 'assert';
5+
import { template } from '../lib/micro-template.js';
6+
import ejs from 'ejs';
7+
import fs from 'fs';
8+
import os from 'os';
59

610
// ============================================================================
711
// Simple JavaScript Templating
@@ -38,16 +42,20 @@ var template = require('../lib/micro-template.js').template;
3842
// Provide some basic currying to the user
3943
return data ? fn( data ) : fn;
4044
};
41-
})();
45+
}).call(global);
4246
// ============================================================================
43-
var ejs = require('ejs');
47+
const fizzbuzz = fs.readFileSync('test/data-fizzbuzz.tmpl', 'utf-8');
48+
const fizzbuzzRaw1 = fizzbuzz.replace(/<%=/g, '<%=raw');
49+
const fizzbuzzRaw2 = fizzbuzz.replace(/<%=/g, '<%-');
50+
const fizzbuzzVar = fizzbuzz.replace(/^/, '<% var n = stash.n; %>');
51+
const ejsFunc = ejs.compile(fizzbuzzRaw2);
4452

45-
// ============================================================================
46-
var fizzbuzz = require('fs').readFileSync('test/data-fizzbuzz.tmpl', 'utf-8');
47-
var fizzbuzzRaw1 = fizzbuzz.replace(/<%=/g, '<%=raw');
48-
var fizzbuzzRaw2 = fizzbuzz.replace(/<%=/g, '<%-');
49-
var fizzbuzzVar = fizzbuzz.replace(/^/, '<% var n = stash.n; %>');
50-
var ejsFunc = ejs.compile(fizzbuzzRaw2);
53+
const output1 = template(fizzbuzz, {n: 30}).replace(/\s+/g, ' ');
54+
const output2 = ejsFunc({n: 30 }).replace(/\s+/g, ' ');
55+
template.variable = 'stash';
56+
const output3 = template(fizzbuzzVar, {n: 30}).replace(/\s+/g, ' ');
57+
assert.equal(output1, output2, 'output should be same');
58+
assert.equal(output1, output3, 'output should be same');
5159

5260
benchmark({
5361
"micro-template" : function () {
@@ -58,7 +66,7 @@ benchmark({
5866
template.variable = null;
5967
template(fizzbuzz, {n : 300 });
6068
},
61-
"micro-template (template.variable)" : function () {
69+
"micro-template (without `with`)" : function () {
6270
template.variable = 'stash';
6371
template(fizzbuzzVar, {n : 300 });
6472
},
@@ -77,26 +85,25 @@ benchmark({
7785
// ============================================================================
7886
// try n counts in 1sec
7987
function measure (fun) {
80-
var now, start = new Date().getTime();
81-
var count = 0, n = 500;
88+
for (let i = 0; i < 1000; i++) fun(); // warm up
89+
90+
let now, count = 0, n = 500;
91+
const start = new Date().getTime();
8292
do {
83-
for (var i = 0; i < n; i++) fun();
93+
for (let i = 0; i < n; i++) fun();
8494
count += n;
8595
now = new Date().getTime();
8696
} while ( (now - start) < 1000);
8797
return (count / (now - start)) * 1000;
8898
}
8999

90100
function benchmark (funcs) {
91-
var os = require('os');
92-
console.log('%s (%s) %s %s', os.type(), os.platform(), os.arch(), os.release());
93-
console.log(os.cpus());
94-
95-
var empty = 1000 / measure(function () {});
96-
console.log('empty function call: %d msec', empty);
101+
console.log(' A larger number (count) means faster. A smaller number (msec) means faster.');
102+
console.log('%s (%s) %s %s %s %d cpus', os.type(), os.platform(), os.arch(), os.release(), os.cpus()[0].model, os.cpus().length);
103+
// console.log(os.cpus());
97104

98-
var result = [];
99-
for (var key in funcs) if (funcs.hasOwnProperty(key)) {
105+
const result = [];
106+
for (const key of Object.keys(funcs)) {
100107
console.log('running... %s', key);
101108
result.push({ name : key, counts : measure(funcs[key]) });
102109
}
@@ -105,6 +112,6 @@ function benchmark (funcs) {
105112
console.log('=== result ===');
106113

107114
for (var i = 0, it; (it = result[i]); i++) {
108-
console.log("%d: (%d msec) %s", it.counts.toFixed(1), (1000 / it.counts - (empty * it.counts)).toFixed(3), it.name);
115+
console.log("%d: (%d msec) %s", it.counts.toFixed(1), (1000 / it.counts).toFixed(3), it.name);
109116
}
110117
}

0 commit comments

Comments
 (0)