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
470
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
800
What about Temporal in JavaScript
arayaryoma
5
1.5k
var, let, const and immutable of JavaScript
arayaryoma
0
230
2018-07-31-angular-tips
arayaryoma
1
120
Other Decks in Programming
See All in Programming
知られざるDMMデータエンジニアの生態 〜かつてツチノコと呼ばれし者〜
takaha4k
1
460
asdf-ecspresso作って 友達が増えた話 / Fujiwara Tech Conference 2025
koluku
0
1.4k
テストコード書いてみませんか?
onopon
2
340
週次リリースを実現するための グローバルアプリ開発
tera_ny
1
1.2k
Swiftコンパイラ超入門+async関数の仕組み
shiz
0
180
“あなた” の開発を支援する AI エージェント Bedrock Engineer / introducing-bedrock-engineer
gawa
4
430
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
最近のVS Codeで気になるニュース 2025/01
74th
1
110
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
550
自分ひとりから始められる生産性向上の取り組み #でぃーぷらすオオサカ
irof
3
370
Amazon Nova Reelの可能性
hideg
0
200
責務を分離するための例外設計 - PHPカンファレンス 2024
kajitack
9
2.4k
Featured
See All Featured
The Invisible Side of Design
smashingmag
299
50k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
39
1.9k
The Cult of Friendly URLs
andyhume
78
6.1k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
52k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
30
2.1k
Fireside Chat
paigeccino
34
3.1k
Six Lessons from altMBA
skipperchong
27
3.6k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
A designer walks into a library…
pauljervisheath
205
24k
How GitHub (no longer) Works
holman
312
140k
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Ͱͷ࣮