Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
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
460
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
PaaSとSaaSの境目で信頼性と開発速度を両立する 〜TROCCO®︎のこれまでとこれから〜
gtnao
6
6.1k
watsonx.ai Dojo #4 生成AIを使ったアプリ開発、応用編
oniak3ibm
PRO
1
270
As an Engineers, let's build the CRM system via LINE Official Account 2.0
clonn
1
620
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
340
flutterkaigi_2024.pdf
kyoheig3
0
440
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
10
4.1k
[FlutterKaigi2024] Effective Form 〜Flutterによる複雑なフォーム開発の実践〜
chocoyama
0
3.9k
macOS なしで iOS アプリを開発する(※ただし xxx に限る)
mitsuharu
1
160
Cognitoが大型アップデート!Managed Loginとパスワードレスログインを実際に使ってみた@しむそくRadio Special Day1
tmhirai
2
160
気をつけたい!Desktop対応で陥りやすい罠とその対策
goto_tsl
0
180
新卒研修で作ったアプリのご紹介
mkryo
0
230
Leverage LLMs in Java with LangChain4j and Quarkus
hollycummins
0
150
Featured
See All Featured
Into the Great Unknown - MozCon
thekraken
33
1.5k
[RailsConf 2023] Rails as a piece of cake
palkan
52
5k
Designing for humans not robots
tammielis
250
25k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5k
Intergalactic Javascript Robots from Outer Space
tanoku
269
27k
Facilitating Awesome Meetings
lara
50
6.1k
Code Reviewing Like a Champion
maltzj
520
39k
Practical Orchestrator
shlominoach
186
10k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
150
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
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Ͱͷ࣮