Skip to content

Commit 9c6420d

Browse files
refactor(non-blocking-json): use micro task instead
1 parent 35df67b commit 9c6420d

File tree

1 file changed

+14
-21
lines changed
  • packages/node_modules/non-blocking-json/src

1 file changed

+14
-21
lines changed

packages/node_modules/non-blocking-json/src/index.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,27 @@ export const tweak = (batchLength, trackPerf) => {
1515
TRACK_PERF = Boolean(trackPerf);
1616
};
1717

18-
const tick = (typeof window !== 'undefined' && window.requestAnimationFrame) || setTimeout;
1918
const batch = (() => {
2019
let queue = []
21-
let currentLength = 0
2220
let isRunning = false
23-
function run() {
21+
async function run() {
2422
let tickLength = 0
2523
isRunning = true
26-
tick(() => {
27-
28-
while (currentLength && tickLength < BATCH_LENGTH) {
29-
const queueItem = queue.shift()
30-
queueItem.cb()
31-
tickLength += queueItem.length
32-
currentLength -= queueItem.length
33-
}
24+
await Promise.resolve()
25+
while (queue.length && tickLength < BATCH_LENGTH) {
26+
const queueItem = queue.shift()
27+
queueItem.cb()
28+
tickLength += queueItem.length
29+
}
3430

35-
if (queue.length) {
36-
run()
37-
} else {
38-
isRunning = false
39-
}
40-
})
31+
if (queue.length) {
32+
run()
33+
} else {
34+
isRunning = false
35+
}
4136
}
4237

4338
return (length, cb) => {
44-
currentLength += length
45-
4639
queue.push({
4740
cb,
4841
length
@@ -177,10 +170,10 @@ export function stringify(value, replacer?) {
177170
}
178171

179172
if (index === batches.length - 1) {
180-
resolve(string);
181173
if (TRACK_PERF) {
182174
console.log("STRINGIFY TIME - ", performance.now() - start, paths.size)
183175
}
176+
resolve(string);
184177
}
185178
});
186179
})
@@ -297,10 +290,10 @@ export async function parse(value) {
297290
// We batch the work by using the initial length of the value. This gives us an indication
298291
// of the number of objects and arrays that will need to be parsed
299292
batch(initialLength, () => {
300-
resolve(produceResult(initialValue))
301293
if (TRACK_PERF) {
302294
console.log("PARSE TIME - ", performance.now() - start)
303295
}
296+
resolve(produceResult(initialValue))
304297
})
305298

306299
})

0 commit comments

Comments
 (0)