guo
2023 年4 月 14 日 06:08
1
简单计算性能对比:
bun的性能有点夸张啊。。。
gxy@x1c:~/tmp/bun$ time node test.js
avg= 8.914138680538031e-7
real 1m14.575s
user 1m41.410s
sys 0m3.574s
gxy@x1c:~/tmp/bun$ time qjs test.js
avg= 9.886412477566608e-7
real 0m35.836s
user 0m35.170s
sys 0m0.360s
gxy@x1c:~/tmp/bun$ time deno run test.js
avg= 9.817291988234444e-7
real 0m49.849s
user 0m50.780s
sys 0m1.614s
gxy@x1c:~/tmp/bun$ time bun test.js
avg= 0.3678743069661792
real 0m10.866s
user 0m13.523s
sys 0m0.583s
gxy@x1c:~/tmp/bun$
gxy@x1c:~/tmp/bun$ deno --version
deno 1.32.4 (release, x86_64-unknown-linux-gnu)
v8 11.2.214.9
typescript 5.0.3
gxy@x1c:~/tmp/bun$ bun --version
0.5.9
gxy@x1c:~/tmp/bun$ node -v
v10.24.1
源代码:
var avg = 0;
var list = [];
for (var i = 0; i < 10000000; i++) {
var n = Math.random();
list.push(n);
}
list.sort();
var i = 0;
list.forEach(function (e) {
avg = (avg * i + e) / (i + 1);
});
console.log("avg=", avg);
guo
2023 年4 月 14 日 06:26
2
又跑了一下斐波那契数。
gxy@x1c:~/tmp/bun$ time deno run fibonacci.js
0. 44 : 1134903170
0. 44 : 1134903170
0. 44 : 1134903170
0ms 0ms 8915ms
real 0m8.945s
user 0m8.906s
sys 0m0.007s
gxy@x1c:~/tmp/bun$ time qjs fibonacci.js
0. 44 : 1134903170
0. 44 : 1134903170
0. 44 : 1134903170
0ms 0ms 100704ms
real 1m40.708s
user 1m40.098s
sys 0m0.043s
gxy@x1c:~/tmp/bun$ time node fibonacci.js
0. 44 : 1134903170
0. 44 : 1134903170
0. 44 : 1134903170
1ms 1ms 8037ms
real 0m8.115s
user 0m8.071s
sys 0m0.014s
gxy@x1c:~/tmp/bun$ time bun fibonacci.js
0. 44 : 1134903170
0. 44 : 1134903170
0. 44 : 1134903170
1ms 0ms 5924ms
real 0m5.938s
user 0m5.918s
sys 0m0.010s
代码:
// usage:
// $ time ./fibonacci.js 40
// 0. 40 : 165580141
// 0. 40 : 165580141
// 5ms 3259ms
//
// real 0m3.310s
// user 0m3.301s
// system 0m0.017s
//fibonacci(40)
//fibonacci2(40)
function fibonacci(n) {
var res;
if (fibonacci.stack[n]) return fibonacci.stack[n];
if (n < 2)
res = 1;
else
res = fibonacci(n - 2) + fibonacci(n - 1);
return (fibonacci.stack[n] = res);
}
fibonacci.stack = {};
function fibonacci2(n) {
if (n < 2) return 1;
return fibonacci2(n - 2) + fibonacci2(n - 1);
}
function fibonacci3(n) {
n = n || 1;
var t = 1, i = 1, n1 = 0, n2 = 1;
for (; i <= n; i++) {
t = n2 + n1;
n1 = n2;
n2 = i < 2 ? 1 : t;
}
return t;
}
var ns = [44]
var s1 = Date.now();
ns.forEach(function (n, i) {
console.log(i + '. ', n, ': ', fibonacci(n));
});
var s2 = Date.now();
var t1 = s2 - s1;
ns.forEach(function (n, i) {
console.log(i + '. ', n, ': ', fibonacci3(n));
});
var s3 = Date.now();
var t2 = s3 - s2;
ns.forEach(function (n, i) {
console.log(i + '. ', n, ': ', fibonacci2(n));
});
var s4 = Date.now();
var t3 = s4 - s3;
console.log(t1 + 'ms', t2 + 'ms', t3 + 'ms');
2 个赞
cireu
2023 年4 月 14 日 19:24
3
看来你还不知道 Bun 没有自己实现 JavaScript 解释器, 用的是 WebKit 的 JavaScriptCore
而 JavaScriptCore 热身速度比 V8 更快, 完全热身之后两边不见得谁更有优势
https://twitter.com/jarredsumner/status/1499225725492076544
3 个赞
v8的性能要比JavaScriptCore好很多很多。