@@ -4,15 +4,34 @@ const IS_ARRAY_END = Symbol("IS_ARRAY_END");
44const IS_OBJECT_START = Symbol ( "IS_OBJECT_START" ) ;
55const IS_OBJECT_END = Symbol ( "IS_OBJECT_END" ) ;
66
7+ let QUEUE_BATCH_COUNT = 10
78let ASYNC_BATCH_COUNT = 3000 ;
89let TRACK_PERF = false ;
910
10- export const tweak = ( batchCount , trackPerf ) => {
11+ export const tweak = ( queueBatchCount , batchCount , trackPerf ) => {
12+ QUEUE_BATCH_COUNT = queueBatchCount
1113 ASYNC_BATCH_COUNT = batchCount ;
1214 TRACK_PERF = Boolean ( trackPerf ) ;
1315} ;
1416
15- const next = ( typeof window !== 'undefined' && window . requestAnimationFrame ) || setTimeout ;
17+ const tick = ( typeof window !== 'undefined' && window . requestAnimationFrame ) || setTimeout ;
18+ const queue = ( ( ) => {
19+ let pending = [ ]
20+ function pick ( ) {
21+ const picked = pending . splice ( 0 , QUEUE_BATCH_COUNT )
22+ picked . forEach ( cb => cb ( ) )
23+ if ( pending . length ) {
24+ tick ( pick )
25+ }
26+ }
27+ return ( cb ) => {
28+ pending . push ( cb )
29+
30+ if ( pending . length === 1 ) {
31+ pick ( )
32+ }
33+ }
34+ } ) ( )
1635
1736/*
1837 This function takes a value which results in a Map of
@@ -65,7 +84,7 @@ function isRef(ref) {
6584*/
6685export function stringify ( value , replacer ?) {
6786 return new Promise < string > ( resolve => {
68- next ( async ( ) => {
87+ queue ( async ( ) => {
6988 const paths = getPaths ( value , new Map ( ) , replacer ? replacer . bind ( global || window ) : replacer ) ;
7089 const structs = [ ] ;
7190 let prevKey = [ ] ;
@@ -85,7 +104,7 @@ export function stringify(value, replacer?) {
85104 console . log ( "PERF - " , end - start )
86105 start = end
87106 }
88- await new Promise ( resolve => next ( resolve ) ) ;
107+ await new Promise ( resolve => queue ( resolve ) ) ;
89108 }
90109
91110 x ++ ;
@@ -168,7 +187,7 @@ export async function parse(value) {
168187 console . log ( "PERF - " , end - start )
169188 start = end
170189 }
171- await new Promise ( resolve => next ( resolve ) ) ;
190+ await new Promise ( resolve => queue ( resolve ) ) ;
172191 }
173192
174193 if ( value [ charIndex ] === '"' && value [ charIndex - 1 ] !== '\\' ) {
@@ -218,7 +237,7 @@ export async function parse(value) {
218237 const keys = Object . keys ( parsedValue ) ;
219238
220239 if ( i && i % ASYNC_BATCH_COUNT === 0 ) {
221- await new Promise ( resolve => next ( resolve ) ) ;
240+ await new Promise ( resolve => queue ( resolve ) ) ;
222241 }
223242
224243 return Promise . all (
0 commit comments