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
5260benchmark ( {
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
7987function 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
90100function 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