Slide 1

Slide 1 text

Bun first impressions 2022/09/05 @ NodeֶԂ 40࣌ݶ໨

Slide 2

Slide 2 text

Twitter: @yosuke_furukawa Github: yosuke-furukawa

Slide 3

Slide 3 text

ࠓ೔͸ bun ʹ͍ͭͯ࿩͠·͢

Slide 4

Slide 4 text

bun ͱ͸ • ৽͍͠ JavaScript Runtime • Node.js, Deno ͱϙδγϣϯతʹ͸ಉ͡ • ಛ௃తͳॴ • JSC • Next.js ͕ಈ͘ • TypeScript Transpiler ಉࠝ

Slide 5

Slide 5 text

લʹࣾ಺Ͱ͸࿩͕͋ͬͨ • 2022/1/19 ࠒʹݹ઒ηϯαʔ͸Ωϟον͍ͯͨ͠ • ͔ͬ͢Γ๨Εͯͨɺࠓࠒʹͳͬͯ࿩୊ʹͳͬͨͷ͸ɺ৽͘͠ Next.jsಈ͘Α͏ʹͳͬͨͷͱ͋Δఔ౓όʔδϣϯ্͕͕ͬͯ ࢖͑ΔΑ͏ʹͳͬͨͷͰLPΛ৽͔ͨ͘͠͠ΒͩͱࢥΘΕΔ

Slide 6

Slide 6 text

ͳΜͰ bun ͍ͬͯ͏ͷʁ • bun ͸ bundle ͷ bun ͱͦΕΒΛแΈࠐΉΈ ͍ͨͳҙຯ͔Βத՚·ΜͷΠϝʔδ͕औΒΕ ͨΒ͍͠ɻ • YouTube Ͱݴ͚ͬͯͨͩͳͷͰɺެࣜʹ͸ॻ ͍ͯͳ͍ɻ΋͔ͨ͠͠Βͦͷ͏ͪɺ͔ͬͪΐ ͍͍ཧ༝͕͚ͭΒΕΔ͔΋ɻ

Slide 7

Slide 7 text

bun.sh ͷ໨ࢦ͢ॴ • ߴ଎ʹىಈ͢Δ͜ͱʢEdgeͰಈ͔͢ͷ໨తʣ • ࣍ੈ୅ύϑΥʔϚϯεʢJSCʣ • πʔϧͱͯ͠ great and complete Λ໨ࢦ͢ ʢbundler, transpiler, package manager ͳ Ͳʣ

Slide 8

Slide 8 text

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.

Slide 9

Slide 9 text

bun.sh ͷ໨ࢦ͢ॴ • खݩͷPCͩΖ͏ͱΤοδͩΖ͏ͱશ෦JS/TSͷϥ ϯλΠϜ؀ڥΛஔ͖׵͑ΒΕΔΑ͏ʹ͍ͨ͠ • ͦͷͨΊʹɺ Web API ͱ Node.js API Λ྆ํ͋Δ ఔ౓ಈ͘ͱ͜Ζ·Ͱαϙʔτ͍ͯ͠Δ • Next.js ͕ಈ͘ͷ͸ͦͷ͓͔͛ɺ͜ͷลΓ͕ Deno ͱ͔ͱ͸एׯҧ͏ϚʔέςΟϯάઓུ

Slide 10

Slide 10 text

bun.sh ͷ໨ࢦ͢ॴ • Web API • Fetch • WebSocket • ReadableStream • ͜ΕΒΛ࣮૷ࡁΈ

Slide 11

Slide 11 text

bun.sh ͷ໨ࢦ͢ॴ • node_modules ͷ module resolution algorithm ΋࠷ ॳ͔Β࣮૷͍ͯ͠Δ • ͭ·Γɺ Node.js ͷϞδϡʔϧղܾ΋ಈ͘ • ΋ͪΖΜESM΋ಈ͘ • ESMͷ͕ୈҰࢢຽײ͕͋ΔʢESM and CommonJS are supported, but Bun internally uses ESMʣ

Slide 12

Slide 12 text

bun.sh ͷ໨ࢦ͢ॴ • TypeScript / JSX Ͱॻ͍ͨΒͦΕΛͦͷ··τ ϥϯεύΠϧ͢Δ • APIͰ΋ Bun.Transpiler ͷΑ͏ͳܗͰݺͼग़ͤ Δʢͭ·Γɺࣗ෼ͰͳΜ͔ϓϥάΠϯͱ͔΋ ͔͚Δʣ

Slide 13

Slide 13 text

bun.sh ͷ໨ࢦ͢ॴ • ͳΜ͔Θ͔ΒΜ͚Ͳɺ sqlite Λ࠷ॳ͔Βಉࠝ ͍ͯ͠Δɻ import sqlite from "bun:sqlite"; • `.env` ϑΝΠϧͰ؀ڥม਺ϩʔυ͢Δͱ͔΋࠷ ॳ͔Β΍ͬͯ͘ΕͯΔ • ͜ͷลΓ͸DXతͳ࿩͔ɾɾɾʁ

Slide 14

Slide 14 text

bun.sh ͷ໨ࢦ͢ॴ • Node APIʢn-apiʣ Λ࣮૷ࡁΈ • Node.js ͷnative modules ΋͍͍ͩͨಈ͘ Αʂͱͷ͜ͱɻ • bun:ffi ΋͋ΔɻFFIݺͼग़͠Ͱ΋ native module ͕ಈ͘

Slide 15

Slide 15 text

ύϑΥʔϚϯε • ৭ʑ଎͍ͱ͍͏৮ΕࠐΈ • σϞͷϖʔδͰ͸଎͔ͬͨ • Α͘Α͘ݟΔͱ...

Slide 16

Slide 16 text

ύϑΥʔϚϯε

Slide 17

Slide 17 text

ύϑΥʔϚϯε ΍ͬͯΔͷ͜Ε͚ͩɺSFOEFS5P3FBEBCMF4USFBNͷ͔ࠩʁ

Slide 18

Slide 18 text

ͳΜͰ bun ͸଎͍ͷ͔ • V8 Ͱ͸ͳ͘ JSC Λ࠾༻ͨ͠ͱ͍͏ͱ͜Ζͱ಺ ෦ͷίʔυʹ zig Λ࠾༻ͨ͠ͷ͕ຯḩͱ͍͏ ࿩ • JSCͷ͕ϝϞϦతʹޮ཰͕ྑ͍&JITͷ΍Γํ ͕ҧ͏ɻ

Slide 19

Slide 19 text

bun ͷத਎ʹഭΔ • ߏ੒ਤ • ҰݸҰݸൺֱ͢Δ

Slide 20

Slide 20 text

MJCVW IUUQQBSTFS 7 0QFO44- [MJC OHIUUQ OBQJ 4UBOEBSE-JCSBSJFT +BWB4DSJQU Node.js inside OHUDQ

Slide 21

Slide 21 text

LRVFVFJPVSJOH QJDPIUUQQBSTFS +BWB4DSJQU$PSF #PSJOH44- [MJC OBQJ 4UBOEBSE-JCSBSJFT +BWB4DSJQU CVOKT bun inside IUUQ RVJD૚͸ະ࣮૷

Slide 22

Slide 22 text

node / bun

Slide 23

Slide 23 text

node / bun 4UBOEBSEMJCSBSZ

Slide 24

Slide 24 text

Standard Library • Node.js ͱ bun ͸ҰԠޓ׵ੑ͕͋Δɻ • ͱ͍͏͔ࠓ࣋ͬͯઈࢍNode.jsޓ׵ͷAPIΛͨ͘͞Μ࡞ͬͯΔ •

Slide 25

Slide 25 text

Standard Library • ࣗ෼͕ࢼͨ͠ͱ͖͸ Buffer ͕શવಈ͔ͳ͔ͬ ͨɻ • ·͞ʹ͜Εɻ

Slide 26

Slide 26 text

Standard Library • ઈࢍ௥ՃதͳͷͰɺͦͷ͏ͪ׬શޓ׵ʹͳΔ͔ ΋ɻ • ҰํͰຖճ௥ै͢ΔͷͭΒͦ͏ͩͳͱ΋ࢥ͏ɻ • WINTER CG ϕʔεͷWeb API͕ྲྀߦΕ͹ͦΕ ͚ͩαϙʔτͯ͠શ෦αϙʔτ͢Δͷ͸ແ͠ʹ ͳΔΜ͡Όͳ͍͔ɻɻɻʁͱ͍͏૝૾

Slide 27

Slide 27 text

node / bun "#*૚ͷந৅Խ΋αϙʔτ ͞ΕͯΔڻ͍ͨ

Slide 28

Slide 28 text

N-API • Node.js ͔Β C++, C ͳͲͷωΠςΟϒϞδϡʔ ϧΛݺͼग़࣌͢ͷந৅ԽϨΠϠʔ • bun Ͱطʹ࣮૷͞ΕͯΔͷͰɺ͋Δఔ౓ Node.js ͷ N-API Ͱ࡞ΒΕͨωΠςΟϒϞ δϡʔϧͳΒಈ͘ʢཧ࿦্͸ʣ • ·ͩະݕূ

Slide 29

Slide 29 text

node / bun IUUQQBSTFS͕ҧ͏

Slide 30

Slide 30 text

http parser vs pico http parser • Node.js ͷ http parser ͸ TS Ͱॻ͍ͨΒ C͕ ग़ͯ͘Δมଶύʔαʢindutny੡ʣ • pico http parser ͸CۀքͰ͸୭΋͕࢖ͬͨ͜ ͱ͋Δ kazuho ੡ߴ଎खܰύʔαʔ

Slide 31

Slide 31 text

node / bun 7WT+4$

Slide 32

Slide 32 text

JavaScript Τϯδϯ • JavaScript Λ࣮ߦ͢ΔΤϯδϯ • ࠷ۙͷΤϯδϯ͸େମ Just In Time ίϯύΠϥ͕͍ͭ ͓ͯΓɺ୯ͳΔΠϯλϓϦλͰ͸ͳ͘ɺ౷ܭతʹঢ়گ Λݟͯߴ଎Խ͞ΕΔ • ͜͜Ͱ͸ུ֓Λղઆ͠ɺΤϯδϯͷؾ࣋ͪΛ஌Δɻ • ͪͳΈʹ৘ใ͕ଟ͍ v8 ͷΤϯδϯΛࢀߟʹ͍ͯ͠Δɻ

Slide 33

Slide 33 text

JavaScript Τϯδϯ GVODUJPOTVN B C \ SFUVSOBC ^ TVN < QBSFO > UPLFOJ[F "45 < QBSFO > 1SPHSBN 'VODUJPO%FDMBSBUJPO *EFOUJpFSTVN QBSBNT *EFOUJpFSC *EFOUJpFSB

Slide 34

Slide 34 text

GVODUJPOTVN B C \ SFUVSOBC ^ TVN ύʔαʔ ΠϯλϓϦλ +*5ίϯύΠϥ JavaScript ந৅ߏจ໦ &YDBBFDBF! $SFBUF6ONBQQFE"SHVNFOUT YDBBFDBG!G4UBSS 4YDBBFDC!G -EB/BNFE1SPQFSUZS <> <> όΠτίʔυ ػցޠ ౷ܭ৘ใ ࠷దԽ ୤࠷దԽ

Slide 35

Slide 35 text

V8 vs JSC • Node.js ͸ v8 • bun ͸ JSC • JIT ͢Δ࣌ʹΪΞʢม଎ػʣͷ਺͕ҧ͏

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

V8 • Google ੡ 3ஈม଎ΪΞ • Ignition: ୯ʹ bytecode Λੜ੒͢Δ • Sparkplug: bytecode ͔Βม਺ղܾɺ୤౶ҥߏจԽ ͳͲΛ࣮ࢪ • Turbofan: ౷ܭ৘ใΛجʹܕϨϕϧͰͷ࠷దԽΛ࣮ ࢪ͢Δ

Slide 38

Slide 38 text

JSC • Apple(Webkit) ੡ 4ஈม଎ΪΞ • LLInt: (1଎) ୯ͳΔ bytecode ੜ੒ • Baseline JIT: (2଎) Sparkplug ͱಉ༷ͷbytecode͔Β؆୯ͳJITੜ ੒ίʔυΛ࡞Δ • DFG JIT: (3଎) σʔλϑϩʔʹجͮ͘࠷దԽΛࢪ͢ɺओʹ໭Γ஋ͷ ܕνΣοΫͳͲΛߦ͍ɺ࠷దԽ͢Δɻ • FTL JIT: (4଎) SSA ʹΑΔ࠷దԽΛࢪ͢ɺ V8 ͩͱ Turbofan ͱಉ༷

Slide 39

Slide 39 text

node / bun *0ந৅ԽϨΠϠ͕ҧ͏

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

libuv vs kqueue/iouring ࣮૷ • Windows ʹ͸ WSL (Windows Subsystems for Linux) ͕ ͋Γɺ ΋͏த਎ΛLinuxͱଊ͍͍͑ͯͷͰ͸આ • ͦ͏ߟ͑Δͱɺந৅Խ͕ඞཁͳͷ͸ Linux ͱ Mac ͚ͩʹ ͳΔɻ • ͡Ό͋ͬͯΜͰɺBunͰ͸ Mac ͸ kqueue ɺ Linux ͸ io_uring ͱ͍͏ग़͠෼͚͚͍ͩͯ͠Δɻ͜͜͸ݱ୅ʹ Ԋͬͨਖ਼͍͠ΞʔΩςΫνϟͳؾ΋͢Δɻ

Slide 42

Slide 42 text

bunͷઓུ • ͜Ε·Ͱॻ͍͖ͯͯ΋෼͔Δ௨Γɺ Node.js Λஔ ͖׵͑ΔͨΊʹେ෼node.js͕Θͷ࣮૷ʹาΈدͬ ͯΔ • Deno͕͜Ε͔Β΍Ζ͏ͱ͍ͯ͠Δ Node friendly Λઌʹ΍͔ͬͯΒདྷͨҹ৅ • ϚʔέςΟϯάઓུతʹ໘ന͍

Slide 43

Slide 43 text

bunͷ͜Ε͔Β • n weeks લʹࢼͨ͠ײͩ͡ͱ·ͩ·ͩɻɻɻ • Node.js ͷ API ʹޓ׵ੑ͕͋Δͱ͸ݴ͑ɺ 100%ʹ͸ఔԕ͍ͱײͨ͡ɻ • ΋͠΋ίϯτϦϏϡʔγϣϯνϟϯεΛ࢕ͬͯ ͍Δਓ͸͔͜͜Β΍ͬͯΈΔͱ͍͍ͷ͔΋ɻ

Slide 44

Slide 44 text

JavaScript ϥϯλΠϜͷ͜Ε͔Β • ࠓ೥͸ઓࠃ࣌୅ͷ࢝·ΓʢNode, Deno, Bun, Cloudflare Workersʣ • एׯࡾࠃ࢙ײ͕͋Δ • ᲇ = Node, ޖ = Deno, ᥭ = Bun తͳɻɻɻ • ࠓ·Ͱ͸ػೳࠩΛແ͘͢͜ͱʹ஫ྗ͞Ε͖ͯͨɻ • ͜Ε͔Βݸਓతʹ͸੺นͷઓ͍ʢࠩผԽʣΛݟͤͯ΄͍͠ɻ

Slide 45

Slide 45 text

JavaScript ϥϯλΠϜͷ͜Ε͔Β • ҰํͰ WINTER CG ͱݺ͹ΕΔɺ΢Σϒޓ׵ ͷ JS ϥϯλΠϜ࢓༷ΛܾΊΑ͏ͱ͍͏ಈ͖΋ ग़͍ͯΔɻ

Slide 46

Slide 46 text

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