Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bun first impressions

Bun first impressions

2022/09/05 に Node 学園で発表した Bun の話です。

Yosuke Furukawa

September 05, 2022
Tweet

More Decks by Yosuke Furukawa

Other Decks in Programming

Transcript

  1. bun ͱ͸ • ৽͍͠ JavaScript Runtime • Node.js, Deno ͱϙδγϣϯతʹ͸ಉ͡

    • ಛ௃తͳॴ • JSC • Next.js ͕ಈ͘ • TypeScript Transpiler ಉࠝ
  2. ͳΜͰ bun ͍ͬͯ͏ͷʁ • bun ͸ bundle ͷ bun ͱͦΕΒΛแΈࠐΉΈ

    ͍ͨͳҙຯ͔Βத՚·ΜͷΠϝʔδ͕औΒΕ ͨΒ͍͠ɻ • YouTube Ͱݴ͚ͬͯͨͩͳͷͰɺެࣜʹ͸ॻ ͍ͯͳ͍ɻ΋͔ͨ͠͠Βͦͷ͏ͪɺ͔ͬͪΐ ͍͍ཧ༝͕͚ͭΒΕΔ͔΋ɻ
  3. bun.sh ͷ໨ࢦ͢ॴ ͜͜ʹ͘͢͝ݱΕͯΔ Bun is designed as a drop-in replacement

    for your current JavaScript & TypeScript apps or scripts — on your local computer, server or on the edge. 
 
 Bun natively implements hundreds of Node.js and Web APIs, including ~90% of Node-API functions (native modules), fs, path, Buffer and more. The goal of Bun is to run most of the worlds JavaScript outside of browsers, bringing performance and complexity enhancements to your future infrastructure, as well as developer productivity through better, simpler tooling.
  4. bun.sh ͷ໨ࢦ͢ॴ • खݩͷPCͩΖ͏ͱΤοδͩΖ͏ͱશ෦JS/TSͷϥ ϯλΠϜ؀ڥΛஔ͖׵͑ΒΕΔΑ͏ʹ͍ͨ͠ • ͦͷͨΊʹɺ Web API ͱ

    Node.js API Λ྆ํ͋Δ ఔ౓ಈ͘ͱ͜Ζ·Ͱαϙʔτ͍ͯ͠Δ • Next.js ͕ಈ͘ͷ͸ͦͷ͓͔͛ɺ͜ͷลΓ͕ Deno ͱ͔ͱ͸एׯҧ͏ϚʔέςΟϯάઓུ
  5. bun.sh ͷ໨ࢦ͢ॴ • Web API • Fetch • WebSocket •

    ReadableStream • ͜ΕΒΛ࣮૷ࡁΈ
  6. bun.sh ͷ໨ࢦ͢ॴ • node_modules ͷ module resolution algorithm ΋࠷ ॳ͔Β࣮૷͍ͯ͠Δ

    • ͭ·Γɺ Node.js ͷϞδϡʔϧղܾ΋ಈ͘ • ΋ͪΖΜESM΋ಈ͘ • ESMͷ͕ୈҰࢢຽײ͕͋ΔʢESM and CommonJS are supported, but Bun internally uses ESMʣ
  7. bun.sh ͷ໨ࢦ͢ॴ • TypeScript / JSX Ͱॻ͍ͨΒͦΕΛͦͷ··τ ϥϯεύΠϧ͢Δ • APIͰ΋

    Bun.Transpiler ͷΑ͏ͳܗͰݺͼग़ͤ Δʢͭ·Γɺࣗ෼ͰͳΜ͔ϓϥάΠϯͱ͔΋ ͔͚Δʣ
  8. bun.sh ͷ໨ࢦ͢ॴ • ͳΜ͔Θ͔ΒΜ͚Ͳɺ sqlite Λ࠷ॳ͔Βಉࠝ ͍ͯ͠Δɻ import sqlite from

    "bun:sqlite"; • `.env` ϑΝΠϧͰ؀ڥม਺ϩʔυ͢Δͱ͔΋࠷ ॳ͔Β΍ͬͯ͘ΕͯΔ • ͜ͷลΓ͸DXతͳ࿩͔ɾɾɾʁ
  9. bun.sh ͷ໨ࢦ͢ॴ • Node APIʢn-apiʣ Λ࣮૷ࡁΈ • Node.js ͷnative modules

    ΋͍͍ͩͨಈ͘ Αʂͱͷ͜ͱɻ • bun:ffi ΋͋ΔɻFFIݺͼग़͠Ͱ΋ native module ͕ಈ͘
  10. ͳΜͰ bun ͸଎͍ͷ͔ • V8 Ͱ͸ͳ͘ JSC Λ࠾༻ͨ͠ͱ͍͏ͱ͜Ζͱ಺ ෦ͷίʔυʹ zig

    Λ࠾༻ͨ͠ͷ͕ຯḩͱ͍͏ ࿩ • JSCͷ͕ϝϞϦతʹޮ཰͕ྑ͍&JITͷ΍Γํ ͕ҧ͏ɻ
  11. Standard Library • ઈࢍ௥ՃதͳͷͰɺͦͷ͏ͪ׬શޓ׵ʹͳΔ͔ ΋ɻ • ҰํͰຖճ௥ै͢ΔͷͭΒͦ͏ͩͳͱ΋ࢥ͏ɻ • WINTER CG

    ϕʔεͷWeb API͕ྲྀߦΕ͹ͦΕ ͚ͩαϙʔτͯ͠શ෦αϙʔτ͢Δͷ͸ແ͠ʹ ͳΔΜ͡Όͳ͍͔ɻɻɻʁͱ͍͏૝૾
  12. N-API • Node.js ͔Β C++, C ͳͲͷωΠςΟϒϞδϡʔ ϧΛݺͼग़࣌͢ͷந৅ԽϨΠϠʔ • bun

    Ͱطʹ࣮૷͞ΕͯΔͷͰɺ͋Δఔ౓ Node.js ͷ N-API Ͱ࡞ΒΕͨωΠςΟϒϞ δϡʔϧͳΒಈ͘ʢཧ࿦্͸ʣ • ·ͩະݕূ
  13. http parser vs pico http parser • Node.js ͷ http

    parser ͸ TS Ͱॻ͍ͨΒ C͕ ग़ͯ͘Δมଶύʔαʢindutny੡ʣ • pico http parser ͸CۀքͰ͸୭΋͕࢖ͬͨ͜ ͱ͋Δ kazuho ੡ߴ଎खܰύʔαʔ
  14. JavaScript Τϯδϯ • JavaScript Λ࣮ߦ͢ΔΤϯδϯ • ࠷ۙͷΤϯδϯ͸େମ Just In Time

    ίϯύΠϥ͕͍ͭ ͓ͯΓɺ୯ͳΔΠϯλϓϦλͰ͸ͳ͘ɺ౷ܭతʹঢ়گ Λݟͯߴ଎Խ͞ΕΔ • ͜͜Ͱ͸ུ֓Λղઆ͠ɺΤϯδϯͷؾ࣋ͪΛ஌Δɻ • ͪͳΈʹ৘ใ͕ଟ͍ v8 ͷΤϯδϯΛࢀߟʹ͍ͯ͠Δɻ
  15. JavaScript Τϯδϯ GVODUJPOTVN B C \ SFUVSOB C ^ TVN

       <GVODUJPO LFZXPSE > <TVN JEFOUJpFS > <  QBSFO > <B JEFOUJpFS > <C JEFOUJpFS > UPLFOJ[F "45 <GVODUJPO LFZXPSE > <TVN JEFOUJpFS > <  QBSFO > <B JEFOUJpFS > <C JEFOUJpFS > 1SPHSBN 'VODUJPO%FDMBSBUJPO *EFOUJpFSTVN QBSBNT *EFOUJpFSC *EFOUJpFSB
  16. GVODUJPOTVN B C \ SFUVSOB C ^ TVN  

     ύʔαʔ ΠϯλϓϦλ +*5ίϯύΠϥ JavaScript ந৅ߏจ໦   &YDBBFDBF! $SFBUF6ONBQQFE"SHVNFOUT YDBBFDBG!G4UBSS 4YDBBFDC!G -EB/BNFE1SPQFSUZS <> <> όΠτίʔυ ػցޠ ౷ܭ৘ใ ࠷దԽ ୤࠷దԽ
  17. V8 vs JSC • Node.js ͸ v8 • bun ͸

    JSC • JIT ͢Δ࣌ʹΪΞʢม଎ػʣͷ਺͕ҧ͏
  18. JSC vs V8 • ͦ΋ͦ΋ΪΞ͸ • ௿଎: Ճ଎͸଎͍ɺ࠷ߴ଎͸஗͍ • ߴ଎:

    Ճ଎͸஗͍ɺ࠷ߴ଎͸଎͍ • ͦͷ্Ͱঢ়گʹԠͯ͡ΪΞΛ੾Γସ͑Δඞཁ͕͋ΔɻҰ ճ࠷ߴ଎ʹͨ͠ΒͣͬͱετϨʔτͰ଎͍΋ͷ΋͋Ε ͹ɺϥϦʔͷΑ͏ʹ੾Γฦ͕͠ଟ͍ίʔε΋༗Δͷͱಉ ༷ʹɺ࠷ߴ଎Ͱͣͬͱ૸Γଓ͚ΒΕΔΘ͚Ͱ͸ͳ͍
  19. V8 • Google ੡ 3ஈม଎ΪΞ • Ignition: ୯ʹ bytecode Λੜ੒͢Δ

    • Sparkplug: bytecode ͔Βม਺ղܾɺ୤౶ҥߏจԽ ͳͲΛ࣮ࢪ • Turbofan: ౷ܭ৘ใΛجʹܕϨϕϧͰͷ࠷దԽΛ࣮ ࢪ͢Δ
  20. JSC • Apple(Webkit) ੡ 4ஈม଎ΪΞ • LLInt: (1଎) ୯ͳΔ bytecode

    ੜ੒ • Baseline JIT: (2଎) Sparkplug ͱಉ༷ͷbytecode͔Β؆୯ͳJITੜ ੒ίʔυΛ࡞Δ • DFG JIT: (3଎) σʔλϑϩʔʹجͮ͘࠷దԽΛࢪ͢ɺओʹ໭Γ஋ͷ ܕνΣοΫͳͲΛߦ͍ɺ࠷దԽ͢Δɻ • FTL JIT: (4଎) SSA ʹΑΔ࠷దԽΛࢪ͢ɺ V8 ͩͱ Turbofan ͱಉ༷
  21. libuv vs kqueue/iouring ࣮૷ • libuv ͸ Windows ͱ͔΋αϙʔτ͢ΔͨΊʹ IOCP

    / epoll / kqueue Λந৅Խͯ͠ɺҰͭͷAPI͔Βݺͼग़ͤΔΑ͏ʹ͍ͯ͠ Δɻ • ͨͩ͠ॾʑͷҧ͍Λٵऩ͢ΔͨΊʹɺ಺෦͸ͦΜͳʹ୯७͡Όͳ ͍ɻ • ͲͪΒ͔ͱ͍͏ͱɺඇಉظIOͷϑϨʔϜϫʔΫతͳڊେͳ࣮૷ • ҰํͰɺݱ୅Ͱ͸ͦ͜·Ͱڊେͳ࣮૷͸͍Δͷ͔...? ͱ͍͏໰୊͕ ͋Δɻ
  22. libuv vs kqueue/iouring ࣮૷ • Windows ʹ͸ WSL (Windows Subsystems

    for Linux) ͕ ͋Γɺ ΋͏த਎ΛLinuxͱଊ͍͍͑ͯͷͰ͸આ • ͦ͏ߟ͑Δͱɺந৅Խ͕ඞཁͳͷ͸ Linux ͱ Mac ͚ͩʹ ͳΔɻ • ͡Ό͋ͬͯΜͰɺBunͰ͸ Mac ͸ kqueue ɺ Linux ͸ io_uring ͱ͍͏ग़͠෼͚͚͍ͩͯ͠Δɻ͜͜͸ݱ୅ʹ Ԋͬͨਖ਼͍͠ΞʔΩςΫνϟͳؾ΋͢Δɻ
  23. bunͷ͜Ε͔Β • n weeks લʹࢼͨ͠ײͩ͡ͱ·ͩ·ͩɻɻɻ • Node.js ͷ API ʹޓ׵ੑ͕͋Δͱ͸ݴ͑ɺ

    100%ʹ͸ఔԕ͍ͱײͨ͡ɻ • ΋͠΋ίϯτϦϏϡʔγϣϯνϟϯεΛ࢕ͬͯ ͍Δਓ͸͔͜͜Β΍ͬͯΈΔͱ͍͍ͷ͔΋ɻ
  24. JavaScript ϥϯλΠϜͷ͜Ε͔Β • ࠓ೥͸ઓࠃ࣌୅ͷ࢝·ΓʢNode, Deno, Bun, Cloudflare Workersʣ • एׯࡾࠃ࢙ײ͕͋Δ

    • ᲇ = Node, ޖ = Deno, ᥭ = Bun తͳɻɻɻ • ࠓ·Ͱ͸ػೳࠩΛແ͘͢͜ͱʹ஫ྗ͞Ε͖ͯͨɻ • ͜Ε͔Βݸਓతʹ͸੺นͷઓ͍ʢࠩผԽʣΛݟͤͯ΄͍͠ɻ
  25. JavaScript ϥϯλΠϜͷ͜Ε͔Β • WINTER CG ͩͱ࠷௿ݶ͜͜·Ͱ͋Ε͹ JS ϥϯλΠϜͱͯ͠ͷඞཁ࠷௿ ݶͷ࢓༷ΛຬͨͤΔΑͶɺͱ͍͏࢓༷Λ Web

    ͷ API Λϕʔεʹ࿩͞Εͯ Δɻ • ͜͏͍͏ͷ͕Ͱ͖ͯ͘Δͱɺ΋͏গ͠ϛχϚϜͳ JS ϥϯλΠϜͷ࣮૷Ͱ ΋ͬͱ߈Ίౕ͕ͨग़ͯ͘Δ͔΋͠Εͳ͍ɻ • ଟ༷ੑΛળͱଊ͑ͯɺઓࠃ࣌୅͕ਂ͘ͳΕ͹΋ͬͱք۾͸੝Γ্͕Δ͸ ͣɺͦΕʹ΋ظ଴ɻ • ͪͳΈʹ WINTER CG ͷ James ࢯ͸ jsconf.jp ʹདྷͯ͘ΕΔ͔΋͠Εͳ͍ ͷͰɺੋඇͦͪΒ΋ΑΖ͓͘͠Ͷ͕͍͠·͢ɻ