Created
October 25, 2012 16:11
-
-
Save pasaran/3953729 to your computer and use it in GitHub Desktop.
Compare different eval's in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = function(data) { | |
var s = ''; | |
s += '<div class="hello">'; | |
s += 'Hello, ' + data.username; | |
s += '</div>'; | |
return s; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function(data) { | |
var s = ''; | |
s += '<div class="hello">'; | |
s += 'Hello, ' + data.username; | |
s += '</div>'; | |
return s; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
echo 'require' | |
node test-require.js | |
echo 'function' | |
node test-function.js | |
echo 'vm' | |
node test-vm.js | |
echo 'eval' | |
node test-eval.js | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var js = fs.readFileSync('f2.js', 'utf-8'); | |
var f = eval('(' + js + ')'); | |
var data = { | |
username: 'nop' | |
}; | |
console.time('a'); | |
for (var i = 0; i < 10000000; i++) { | |
var r = f(data); | |
} | |
console.timeEnd('a'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var js = fs.readFileSync('f2.js', 'utf-8'); | |
var f = ( new Function('global', '"use strict"; return (' + js + ')') )(); | |
var data = { | |
username: 'nop' | |
}; | |
console.time('a'); | |
for (var i = 0; i < 10000000; i++) { | |
var r = f(data); | |
} | |
console.timeEnd('a'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var f = require('./f1.js'); | |
var data = { | |
username: 'nop' | |
}; | |
console.time('a'); | |
for (var i = 0; i < 10000000; i++) { | |
var r = f(data); | |
} | |
console.timeEnd('a'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var vm = require('vm'); | |
var js = fs.readFileSync('f2.js', 'utf-8'); | |
var f = vm.runInNewContext('(' + js + ')'); | |
var data = { | |
username: 'nop' | |
}; | |
console.time('a'); | |
for (var i = 0; i < 10000000; i++) { | |
var r = f(data); | |
} | |
console.timeEnd('a'); |
У меня другой результат
~/Public/gist-3953729|master$ node --version
v0.6.11
~/Public/gist-3953729|master$ ./make.sh
require
a: 331ms
function
a: 340ms
vm
a: 348ms
eval
a: 473ms
Обновил ноду
~/Public/gist-3953729|master$ node --version
v0.8.12
~/Public/gist-3953729|master$ ./make.sh
require
a: 12ms
function
a: 12ms
vm
a: 385ms
eval
a: 446ms
Круто, надо обновлять везде :-)
Потому что eval не джитится. Равно как и runInNewContext видимо.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Фига-се. А почему такая разница между eval и new Function?