Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
gotandajs-11-nodejs-recursive-readdir
Search
Araya
May 10, 2019
Programming
2
450
gotandajs-11-nodejs-recursive-readdir
Gotanda.js #11 in MobileFactory (
https://gotandajs.connpass.com/event/127779/
) での発表資料です。
Araya
May 10, 2019
Tweet
Share
More Decks by Araya
See All by Araya
Performance and cache strategy at NIKKEI
arayaryoma
0
2.3k
JavaScript Promise API in 2019
arayaryoma
1
790
What about Temporal in JavaScript
arayaryoma
5
1.5k
var, let, const and immutable of JavaScript
arayaryoma
0
220
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
最新TCAキャッチアップ
0si43
0
140
ピラミッド、アイスクリームコーン、SMURF: 自動テストの最適バランスを求めて / Pyramid Ice-Cream-Cone and SMURF
twada
PRO
10
1.3k
よくできたテンプレート言語として TypeScript + JSX を利用する試み / Using TypeScript + JSX outside of Web Frontend #TSKaigiKansai
izumin5210
5
1.7k
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.1k
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
Better Code Design in PHP
afilina
PRO
0
120
OnlineTestConf: Test Automation Friend or Foe
maaretp
0
100
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
macOS でできる リアルタイム動画像処理
biacco42
9
2.3k
Tauriでネイティブアプリを作りたい
tsucchinoko
0
370
Why Jakarta EE Matters to Spring - and Vice Versa
ivargrimstad
0
1k
Jakarta EE meets AI
ivargrimstad
0
110
Featured
See All Featured
We Have a Design System, Now What?
morganepeng
50
7.2k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
Unsuck your backbone
ammeep
668
57k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.3k
What's in a price? How to price your products and services
michaelherold
243
12k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
25
1.8k
A Philosophy of Restraint
colly
203
16k
Statistics for Hackers
jakevdp
796
220k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
Building Adaptive Systems
keathley
38
2.3k
A Tale of Four Properties
chriscoyier
156
23k
Transcript
Node.jsͰͷrecursive readdirΛߟ͑Δ ͋Β @arayaryoma Gotanda.js #11 in Mobile Factory
ࣗݾհ ͋Β / Ѩ෦ ྇ຏ @arayaryoma @arayaryoma https://araya.dev גࣜձࣾϔϚλΠτ /
෭ۀϑϦʔϥϯε JavaScript/TypeScript/Go/Kotlin Angular/React/ReactNative/Next.js/Nest.js
͔͜͜Βࠇํ໘ʹ10͘Β͍า͍ͨͱ͜ΖʹΦϑΟε͕͋ΔձࣾͰಇ͍͍ͯ·͢ ޒాͷ͖ͳ͓ళϗοτεϓʔϯͰ͢
ୡ͍ͨ͜͠ͱ ͜ͷΑ͏ͳσΟϨΫτϦͰ filesΑΓԼʹ͋ΔશͯͷϑΝΠϧͷϦετΛऔಘ͍ͨ͠
·ͣߟ͑Δ͜ͱ fs.readdirʹΦϓγϣϯΛ͢ͱɺ͔͍͍ͯ͠͠ײ͡ʹऔΕΔͷͰͳ͍͔
·ͣߟ͑Δ͜ͱ વͦΜͳநతͳͷͳ͍
ੈͷதʹస͕͍ͬͯΔղܾࡦ • https://github.com/jergason/recursive-readdir • https://stackoverflow.com/questions/5827612/node-js-fs-readdir-recursive-directory-search • https://gist.github.com/kethinov/6658166 ͳͲ… GoogleͰʮnode.js
recursive readdirʯͱݕࡧ͢Δͱɺ ͍͔ͭ͘ղܾࡦϥΠϒϥϦ͕ݟ͔ͭΔ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ͜ΕΒͷ࣮ྫͰɺ͓͓·͔ʹ 1. fs.readdirͰσΟϨΫτϦԼͷϑΝΠϧͷϦετΛऔಘ͢Δ 2. 1ͷ݁ՌΛϧʔϓΠςϨʔλͰճ͠ɺ֤ϑΝΠϧͷύεΛfs.statؔʹ͠ɺ fs.StatsΦϒδΣΫτΛಘΔ 3. fs.StatsΦϒδΣΫτʹisDirectory()ϝιου͕͋Γɺ͜ΕͰରͷϑΝΠϧ͕ σΟϨΫτϦ͔Ͳ͏͔ఆ͢Δ
4. 3ͷ݁ՌɺσΟϨΫτϦͩͬͨ߹ͦͷσΟϨΫτϦʹ͍ͭͯɺ1~3Λߦ͏ 3ͷ݁ՌɺϑΝΠϧͩͬͨ߹ྻfilesʹՃ͢Δ 5. ϧʔϓ͕ऴΘͬͨΒྻfilesΛฦ͢ ͱ͍͏ํ๏͕ͱΒΕ͍ͯΔɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ΤϥʔϋϯυϦϯάແࢹͨ͠࠷খݶͷ࣮ྫ ಡΈ͢͞ͷͨΊʹಉظ࣮ߦ͞ΕΔؔΛ༻
ੈͷதʹస͕͍ͬͯΔղܾࡦ ݱࡏͷσΟϨΫτϦԼͷϑΝΠϧҰཡΛऔಘ
ੈͷதʹస͕͍ͬͯΔղܾࡦ fs.statSync()Ͱfs.StatsΦϒδΣΫτΛऔಘ͠ɺ σΟϨΫτϦ͔Ͳ͏͔ఆɻ ϑΝΠϧͩͬͨ߹ϑΝΠϧϦετʹՃɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ࢠσΟϨΫτϦͷϦετΛϧʔϓͰճ͠ɺ ֤ࢠσΟϨΫτϦʹରͯؔ͠Λ࠶ؼݺͼग़͢͠Δɻ
ੈͷதʹస͕͍ͬͯΔղܾࡦ ϑΝΠϧͷϦετΛฦ͢
ੈͷதʹస͕͍ͬͯΔղܾࡦ ࣮ߦ݁Ռ
Node.js v10.10Ҏ߱Ͱfs.statΛΘͣʹॻ͚Δ fs.readdir()ʹ{withFileTypes: true}Λ͢ͱɺfilesfs.DirentΦϒδΣΫτͷྻʹͳΔ
Node.js v10.10Ҏ߱Ͱfs.statΛΘͣʹॻ͚Δ fs.DirentΦϒδΣΫτʹisDirectory() isFile()ͳͲͷϝιου͕࣮͞Ε͍ͯΔ ϑΝΠϧ໊.nameͰऔಘͰ͖Δ -> ϑΝΠϧγεςϜίʔϧΛ1σΟϨΫτϦʹ͖ͭ 1Ͱࡁ·͢͜ͱ͕Ͱ͖Δ https://github.com/nodejs/node/pull/22020
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔ఆ 1֊ʹ͖ͭ1ͷϑΝΠϧγεςϜίʔϧ
fs.Dirent ΦϒδΣΫτΛ࣮ͬͨྫ {withFileTypes: true}Λࢦఆͯ͠ fs.DirentΦϒδΣΫτΛಘΔ dirent.isDirectoryͰ σΟϨΫτϦ͔Ͳ͏͔ఆ 1֊ʹ͖ͭ1ͷϑΝΠϧγεςϜίʔϧ
ඇಉظʹ͢Δ fs.readdir()ʹίʔϧόοΫؔΛͯ݁͠ՌΛॲཧ͍͍͕ͯ͠ɺ Node.js v10 ͰɺExperimental͕ͩfs Promises API͕ఏڙ͞Ε͍ͯΔ (v12Ͱstable)
fs Promises API
fs Promises APIΛͬͯॻ͖͢
·ͱΊ •ϥΠϒϥϦΛΘͳͯ͘ɺͦ͜·Ͱଟ͘ͳ͍هड़ྔͰrecursive readdir࣮ݱͰ͖Δɻ •Node.js v10.10Ҏ͕߱͑Δ߹fs.DirentΦϒδΣΫτΛ׆༻ͯ͠ ϑΝΠϧγεςϜίʔϧͷճΛݮΒ͢͜ͱ͕Ͱ͖Δɻ •fs Promises APIΛ༻͍Δ͜ͱͰɺϑΝΠϧγεςϜૢ࡞͕PromiseϕʔεͰॻ͚Δɻ •Node.jsminor
version updateͰศརͳAPI͕Ճ͞ΕΔ͜ͱ͕͋ΔͨΊɺ LTSͷ࠷৽Λ͍ଓ͚Δબࢶ͕औΕΔͱྑ͍ɻ
ิ: direntͱ͍͏֓೦ʹ͍ͭͯ direntLinux Programmingʹ͓͍ͯɺϑΝΠϧͷใΛ࣋ͭߏମɻ readdirؔ͜ͷߏମΛฦ͢ɻ manϖʔδ( http://man7.org/linux/man-pages/man3/readdir.3.html )ΑΓɺglibcͰͷ࣮