Skip to content

Instantly share code, notes, and snippets.

@jimmyeisenhauer
Last active February 25, 2017 18:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimmyeisenhauer/46b1fd875d789daebbff29c22b601bd1 to your computer and use it in GitHub Desktop.
Save jimmyeisenhauer/46b1fd875d789daebbff29c22b601bd1 to your computer and use it in GitHub Desktop.
var Benchmark = require('benchmark');
var escapeRegex = require('escape-string-regexp');
var suite = new Benchmark.Suite;
var text = "Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin \
literature from 45 BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, \
looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in \
classical literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus \
Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse \
very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. \
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus \
Bonorum et mouse Malorum by Cicero are also mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse \
1914 translation by H. Rackham. Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical \
Latin literature from 45 BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, \
looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical \
literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et \
Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse very popular \
during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard chunk of \
Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse Malorum \
by Cicero are also mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation by H. Rackham. \
Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin literature from 45 \
BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the \
more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical \
literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus \
Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of \
ethics, mouse very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line \
in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 \
and 1.10.33 from de Finibus Bonorum et mouse Malorum by Cicero are also mouse reproduced in their exact original form, accompanied by \
mouse English versions from the mouse 1914 translation by H. Rackham. Contrary to popular belief, Lorem Ipsum is not simply random text. \
mouse It has roots in a mouse piece of classical Latin literature from 45 BC, making it over 2000 mouse years old. Richard McClintock, a \
Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum mouse \
passage, and going through the cites of the word in classical literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from \
sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a \
treatise on the theory of ethics, mouse very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes \
from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 \
and 1.10.33 from de Finibus Bonorum et mouse Malorum by Cicero are also mouse reproduced in their exact original form, accompanied by mouse English \
versions from the mouse 1914 translation by H. Rackham. Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in \
a mouse piece of classical Latin literature from 45 BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney \
College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of \
the word in classical literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de \
Finibus Bonorum et Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse \
very popular during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard \
chunk of Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse \
Malorum by Cicero are also mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation \
by H. Rackham. Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin \
literature from 45 BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked \
up one of the more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical \
literature, fish discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et \
Malorum (The Extremes of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse very popular \
during the Renaissance. The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard chunk of \
Lorem Ipsum used since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse Malorum \
by Cicero are also mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation by H. \
Rackham. Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin literature \
from 45 BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of \
the more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical literature, mouse \
discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of \
Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse very popular during the Renaissance. The \
first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the \
1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse Malorum by Cicero are also mouse \
reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation by H. Rackham. Contrary to \
popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin literature from 45 BC, making \
it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure \
Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical literature, fish discovered \
the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes of \
Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse very popular during the Renaissance. \
The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used \
since the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse Malorum by Cicero \
are also mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation by H. Rackham. \
Contrary to popular belief, Lorem Ipsum is not simply random text. mouse It has roots in a mouse piece of classical Latin literature from 45 \
BC, making it over 2000 mouse years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the \
more obscure Latin words, consectetur, from a Lorem Ipsum mouse passage, and going through the cites of the word in classical literature, fish \
discovered the mouse undoubtable source. Lorem mouse Ipsum comes from sections 1.10.32 and 1.10.33 of de Finibus Bonorum et Malorum (The Extremes \
of Good and Evil) by Cicero, mouse written in 45 BC. This book is a treatise on the theory of ethics, mouse very popular during the Renaissance. \
The first line of Lorem Ipsum, Lorem ipsum dolor sit amet.., comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since \
the 1500s is reproduced below for those mouse interd. Sections 1.10.32 and 1.10.33 from de Finibus Bonorum et mouse Malorum by Cicero are also \
mouse reproduced in their exact original form, accompanied by mouse English versions from the mouse 1914 translation by H. Rackham. "
var key = "mouse"
var value = "fish"
// add tests
suite.add('RegExpReplace', function() {
var text1 = text.replace(new RegExp(key, 'g'), value);
})
.add('SplitJoin', function() {
var text2 = text.split(key).join(value);
})
.add('Loop', function() {
var text3 = text
while (text3.indexOf(key) > -1) {
text3 = text3.replace(key, value);
}
})
.add('RegExpReplaceEscape', function() {
var text4 = text.replace(new RegExp(escapeRegex(key), 'g'), value);
})
.add('RegExpReplaceControlRun', function() {
var text5 = text.replace(new RegExp(key, 'g'), value);
})
.add('LoopControlRun', function() {
var text6 = text
while (text6.indexOf(key) > -1) {
text6 = text6.replace(key, value);
}
})
.add('SplitJoinControlRun', function() {
var text7 = text.split(key).join(value);
})
// add listeners
.on('cycle', function(event) {
console.log(String(event.target));
})
.on('complete', function() {
console.log('Fastest is ' + this.filter('fastest').map('name'));
})
// run async
.run({ 'async': true });
@jimmyeisenhauer
Copy link
Author

jimmyeisenhauer commented Feb 25, 2017

benchBokor node --version
v6.10.0
benchBokor node bench.js
RegExpReplace x 163,879 ops/sec ±0.57% (91 runs sampled)
SplitJoin x 291,085 ops/sec ±0.82% (92 runs sampled)
Loop x 2,596 ops/sec ±0.40% (95 runs sampled)
RegExpReplaceEscape x 161,572 ops/sec ±0.39% (96 runs sampled)
RegExpReplaceControlRun x 165,887 ops/sec ±0.39% (96 runs sampled)
Fastest is SplitJoin
➜ benchBokor node bench.js
RegExpReplace x 164,744 ops/sec ±0.53% (93 runs sampled)
SplitJoin x 292,714 ops/sec ±0.86% (93 runs sampled)
Loop x 2,585 ops/sec ±0.72% (95 runs sampled)
RegExpReplaceEscape x 161,377 ops/sec ±0.41% (88 runs sampled)
RegExpReplaceControlRun x 165,443 ops/sec ±0.52% (94 runs sampled)
LoopControlRun x 2,589 ops/sec ±0.49% (94 runs sampled)
SplitJoinControlRun x 295,974 ops/sec ±0.62% (93 runs sampled)
Fastest is SplitJoinControlRun

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment