mirror of
https://github.com/nodejs/node.git
synced 2024-11-21 10:59:27 +00:00
fs: refactoring declaratively with Array.fromAsync
Refs: https://github.com/nodejs/node/pull/51912 PR-URL: https://github.com/nodejs/node/pull/54644 Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
This commit is contained in:
parent
772b35bdc4
commit
090add7864
37
benchmark/fs/bench-glob.js
Normal file
37
benchmark/fs/bench-glob.js
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const common = require('../common');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const assert = require('node:assert');
|
||||||
|
|
||||||
|
const benchmarkDirectory = path.resolve(__dirname, '..', '..');
|
||||||
|
|
||||||
|
const configs = {
|
||||||
|
n: [1e3],
|
||||||
|
dir: ['lib'],
|
||||||
|
pattern: ['**/*', '*.js', '**/**.js'],
|
||||||
|
mode: ['async', 'sync'],
|
||||||
|
recursive: ['true', 'false'],
|
||||||
|
};
|
||||||
|
|
||||||
|
const bench = common.createBenchmark(main, configs);
|
||||||
|
|
||||||
|
async function main(config) {
|
||||||
|
const fullPath = path.resolve(benchmarkDirectory, config.dir);
|
||||||
|
const { pattern, recursive, mode } = config;
|
||||||
|
|
||||||
|
let noDead;
|
||||||
|
bench.start();
|
||||||
|
|
||||||
|
for (let i = 0; i < config.n; i++) {
|
||||||
|
if (mode === 'async') {
|
||||||
|
noDead = await fs.promises.glob(pattern, { cwd: fullPath, recursive });
|
||||||
|
} else {
|
||||||
|
noDead = fs.globSync(pattern, { cwd: fullPath, recursive });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bench.end(config.n);
|
||||||
|
assert.ok(noDead);
|
||||||
|
}
|
@ -25,6 +25,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const {
|
const {
|
||||||
|
ArrayFromAsync,
|
||||||
ArrayPrototypePush,
|
ArrayPrototypePush,
|
||||||
BigIntPrototypeToString,
|
BigIntPrototypeToString,
|
||||||
Boolean,
|
Boolean,
|
||||||
@ -3115,10 +3116,7 @@ function glob(pattern, options, callback) {
|
|||||||
// TODO: Use iterator helpers when available
|
// TODO: Use iterator helpers when available
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
const res = [];
|
const res = await ArrayFromAsync(new Glob(pattern, options).glob());
|
||||||
for await (const entry of new Glob(pattern, options).glob()) {
|
|
||||||
ArrayPrototypePush(res, entry);
|
|
||||||
}
|
|
||||||
callback(null, res);
|
callback(null, res);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
callback(err);
|
callback(err);
|
||||||
|
1
typings/primordials.d.ts
vendored
1
typings/primordials.d.ts
vendored
@ -111,6 +111,7 @@ declare namespace primordials {
|
|||||||
export const ArrayPrototype: typeof Array.prototype
|
export const ArrayPrototype: typeof Array.prototype
|
||||||
export const ArrayIsArray: typeof Array.isArray
|
export const ArrayIsArray: typeof Array.isArray
|
||||||
export const ArrayFrom: typeof Array.from
|
export const ArrayFrom: typeof Array.from
|
||||||
|
export const ArrayFromAsync: typeof Array.fromAsync
|
||||||
export const ArrayOf: typeof Array.of
|
export const ArrayOf: typeof Array.of
|
||||||
export const ArrayPrototypeConcat: UncurryThis<typeof Array.prototype.concat>
|
export const ArrayPrototypeConcat: UncurryThis<typeof Array.prototype.concat>
|
||||||
export const ArrayPrototypeCopyWithin: UncurryThis<typeof Array.prototype.copyWithin>
|
export const ArrayPrototypeCopyWithin: UncurryThis<typeof Array.prototype.copyWithin>
|
||||||
|
Loading…
Reference in New Issue
Block a user