Skip to content

Conversation

@cho45
Copy link
Owner

@cho45 cho45 commented Jun 14, 2025

同じコードが走ると最適化されるみたいなベンチの問題かも。


name = 'template' な 場合と name = 'template' + xxx な場合とで速さが違いすぎる。

  micro-template pre compiled
   1.02x faster than micro-template
   1.05x faster than micro-template (template.variable)
   1.72x faster than micro-template (not cached)
  micro-template pre compiled
   1.03x faster than micro-template
   1.03x faster than micro-template (template.variable)
   8.57x faster than micro-template (not cached)

挙動の調査

遅い
name. = `template-${Math.random().toString(36).slice(2)}` 8.7x
name = 'template' + (me.n=(me.n||0)+1) 8.25x
name = `template-${++counter}` # 8.54x
name = `template-${Date.now()}` # 6.15x

早い
name = `template-${id.slice(0, 10)}`; 1.72x

非決定的な外部要因が入ると遅くなる。このコード自体の速度とは関係ない

@cho45
Copy link
Owner Author

cho45 commented Jun 14, 2025

name がどうとかっていうより、V8 側の最適化で意図せず早くなっているっぽい。以下のように毎回ちょっと違うテンプレートを渡すと遅くなる

  micro-template pre compiled
   1.04x faster than micro-template
   1.05x faster than micro-template (template.variable)
   8.81x faster than micro-template (not cached)
diff --git a/misc/benchmark.js b/misc/benchmark.js
index fa01902..8cc3f4d 100755
--- a/misc/benchmark.js
+++ b/misc/benchmark.js
@@ -77,6 +77,7 @@ assert.equal(output1, output2, 'output should be same');
 assert.equal(output1, output3, 'output should be same');
 assert.equal(output1, output4, 'output should be same');
 
+let counter = 0;
 barplot(() => {
        summary(() => {
                bench('micro-template', () => {
@@ -89,7 +90,7 @@ barplot(() => {
                bench('micro-template (not cached)', () => {
                        template.variable = null;
                        template.cache.clear();
-                       template(fizzbuzz, { n: 300 });
+                       template(fizzbuzz + (counter++), { n: 300 });
                });
                bench('micro-template (template.variable)', () => {
                        template.variable = 'stash';

@cho45 cho45 changed the title name の指定によってキャッシュ効かない場合のベンチ速度が大きく変化する。 benchmark の not cached が適切ではない Jun 14, 2025
@cho45
Copy link
Owner Author

cho45 commented Jun 14, 2025

こっちは閉じる

@cho45 cho45 closed this Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants