22/**
33 * Globals for benchmark.js
44 */
5- global . escapeHtml = require ( '..' )
5+ var lib = require ( '..' )
6+ global . escapeHtml = lib . escapeHtml
7+ global . escapeHtmlFast = lib . escapeHtmlFast
8+ global . escapeHtmlNoRegex = lib . escapeHtmlNoRegex
69
710/**
811 * Module dependencies.
912 */
1013var benchmark = require ( 'benchmark' )
1114var benchmarks = require ( 'beautify-benchmark' )
15+ var fs = require ( 'fs' )
16+ var hugeHTML = fs . readFileSync ( "mathematica.html" ) . toString ( )
17+
18+ const MIN_SAMPLES = 3
1219
1320for ( var dep in process . versions ) {
1421 console . log ( ' %s@%s' , dep , process . versions [ dep ] )
@@ -17,28 +24,129 @@ for (var dep in process.versions) {
1724console . log ( '' )
1825
1926var suite = new benchmark . Suite ( )
27+ const fn = function ( ) { escapeHtmlFast ( str ) }
28+
29+ // suite.add({
30+ // 'name': 'no special characters',
31+ // 'minSamples': MIN_SAMPLES,
32+ // 'fn': function() { escapeHtml(str) },,
33+ // 'setup': function() { str = "Hello, World!" }
34+ // })
35+
36+ // suite.add({
37+ // 'name': 'no special characters (large)',
38+ // 'minSamples': MIN_SAMPLES,
39+ // 'fn': function() { escapeHtml(str) },,
40+ // 'setup': function() { str = "Hello, World!".repeat(1000) }
41+ // })
42+
2043
2144suite . add ( {
22- 'name' : 'no special characters' ,
23- 'minSamples' : 100 ,
24- 'fn' : 'escapeHtml(str)' ,
25- 'setup' : 'str = "Hello, World!"'
45+ 'name' : 'Long HTML page' ,
46+ 'minSamples' : MIN_SAMPLES ,
47+ 'fn' : function ( ) { escapeHtml ( hugeHTML ) } ,
2648} )
2749
50+ suite . add ( {
51+ 'name' : 'Long HTML page REGEX' ,
52+ 'minSamples' : MIN_SAMPLES ,
53+ 'fn' : function ( ) { escapeHtmlFast ( hugeHTML ) } ,
54+ } )
55+
56+ suite . add ( {
57+ 'name' : 'Short HTML page' ,
58+ 'minSamples' : MIN_SAMPLES ,
59+ 'fn' : function ( ) { escapeHtml ( hugeHTML . substring ( 1 , 30000 ) ) } ,
60+ } )
61+
62+ suite . add ( {
63+ 'name' : 'Short HTML page REGEX' ,
64+ 'minSamples' : MIN_SAMPLES ,
65+ 'fn' : function ( ) { escapeHtmlFast ( hugeHTML . substring ( 1 , 30000 ) ) } ,
66+ } )
67+
68+
2869suite . add ( {
2970 'name' : 'single special character' ,
30- 'minSamples' : 100 ,
31- 'fn' : 'escapeHtml(str)' ,
32- 'setup' : 'str = "Hello, World&!"'
71+ 'minSamples' : MIN_SAMPLES ,
72+ 'fn' : function ( ) { escapeHtml ( str ) } ,
73+ 'setup' : function ( ) { str = "Hello, World&!" }
74+ } )
75+
76+ suite . add ( {
77+ 'name' : 'single special character REGEX' ,
78+ 'minSamples' : MIN_SAMPLES ,
79+ 'fn' : function ( ) { escapeHtmlFast ( str ) } ,
80+ 'setup' : function ( ) { str = "Hello, World&!" }
3381} )
3482
83+ suite . add ( {
84+ 'name' : 'single special character (large)' ,
85+ 'minSamples' : MIN_SAMPLES ,
86+ 'fn' : function ( ) { escapeHtml ( str ) } ,
87+ 'setup' : function ( ) {
88+ str = "Hello, World!" . repeat ( 500 )
89+ + "&"
90+ + "Hello, World!" . repeat ( 500 )
91+ }
92+ } )
93+
94+ suite . add ( {
95+ 'name' : 'single special character (large) REGEX' ,
96+ 'minSamples' : MIN_SAMPLES ,
97+ 'fn' : function ( ) { escapeHtmlFast ( str ) } ,
98+ 'setup' : function ( ) {
99+ str = "Hello, World!" . repeat ( 500 )
100+ + "&"
101+ + "Hello, World!" . repeat ( 500 )
102+ }
103+ } )
104+
105+
35106suite . add ( {
36107 'name' : 'many special characters' ,
37- 'minSamples' : 100 ,
38- 'fn' : 'escapeHtml(str)' ,
39- 'setup' : 'str = "\'>\'\\"\\"&>h<e>&<y>"'
108+ 'minSamples' : MIN_SAMPLES ,
109+ 'fn' : function ( ) { escapeHtml ( str ) } ,
110+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' }
111+ } )
112+
113+ suite . add ( {
114+ 'name' : 'many special characters REGEX' ,
115+ 'minSamples' : MIN_SAMPLES ,
116+ 'fn' : function ( ) { escapeHtmlFast ( str ) } ,
117+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' }
118+ } )
119+
120+ suite . add ( {
121+ 'name' : 'many special characters NO REGEX' ,
122+ 'minSamples' : MIN_SAMPLES ,
123+ 'fn' : function ( ) { escapeHtmlNoRegex ( str ) } ,
124+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' }
125+ } )
126+
127+
128+ suite . add ( {
129+ 'name' : 'many special characters (large)' ,
130+ 'minSamples' : MIN_SAMPLES ,
131+ 'fn' : function ( ) { escapeHtml ( str ) } ,
132+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' . repeat ( 1000 ) }
133+ } )
134+
135+ suite . add ( {
136+ 'name' : 'many special characters (large) REGEX' ,
137+ 'minSamples' : MIN_SAMPLES ,
138+ 'fn' : function ( ) { escapeHtmlFast ( str ) } ,
139+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' . repeat ( 1000 ) }
40140} )
41141
142+ suite . add ( {
143+ 'name' : 'many special characters (large) NO REGEX' ,
144+ 'minSamples' : MIN_SAMPLES ,
145+ 'fn' : function ( ) { escapeHtmlNoRegex ( str ) } ,
146+ 'setup' : function ( ) { str = '\'>\'\\"\\"&>h<e>&<y>"' . repeat ( 1000 ) }
147+ } )
148+
149+
42150suite . on ( 'cycle' , function onCycle ( event ) {
43151 benchmarks . add ( event . target )
44152} )
@@ -47,4 +155,4 @@ suite.on('complete', function onComplete () {
47155 benchmarks . log ( )
48156} )
49157
50- suite . run ( { 'async' : false } )
158+ suite . run ( { 'async' : false , maxTime : 0.001 } )
0 commit comments