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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Потому что eval не джитится. Равно как и runInNewContext видимо.