@@ -274,6 +274,84 @@ function representativeJsonStringifyReplacerMembership(jobs, lane) {
274274 return total ;
275275}
276276
277+ // JSON.stringify's cycle rule is active-ancestor membership, not global graph
278+ // visitation. Selection constructs the graph once before all ten warmups and
279+ // the scored invocation; every job performs and validates a real stringify.
280+ var representativeJsonStringifyGraph = null ;
281+ var representativeJsonStringifyDepth = 0 ;
282+ var representativeJsonStringifyExpectedLength = 0 ;
283+ var representativeJsonStringifyExpectedLeafOffset = 0 ;
284+ function selectRepresentativeJsonStringifyDepth ( depth , circular ) {
285+ var leaf = { leaf : 1 } ;
286+ var root = leaf ;
287+ for ( var level = 1 ; level < depth ; level = level + 1 )
288+ root = { next : root } ;
289+ if ( circular ) leaf . next = root ;
290+ representativeJsonStringifyGraph = root ;
291+ representativeJsonStringifyDepth = depth ;
292+ representativeJsonStringifyExpectedLength = depth * 9 + 1 ;
293+ representativeJsonStringifyExpectedLeafOffset = ( depth - 1 ) * 8 + 1 ;
294+ return circular ? representativeJsonStringifyCycle : representativeJsonStringifyAcyclic ;
295+ }
296+
297+ function representativeJsonStringifyAcyclic ( jobs , lane ) {
298+ var total = 0 ;
299+ for ( var job = 0 ; job < jobs ; job = job + 1 ) {
300+ var encoded = JSON . stringify ( representativeJsonStringifyGraph ) ;
301+ var leafOffset = encoded . indexOf ( "\"leaf\":1" ) ;
302+ if ( encoded . length !== representativeJsonStringifyExpectedLength ||
303+ leafOffset !== representativeJsonStringifyExpectedLeafOffset ||
304+ encoded . charCodeAt ( 0 ) !== 123 || encoded . charCodeAt ( encoded . length - 1 ) !== 125 )
305+ throw new Error ( "JSON.stringify depth witness drift" ) ;
306+ total = total + encoded . length + leafOffset +
307+ encoded . charCodeAt ( 0 ) + encoded . charCodeAt ( encoded . length - 1 ) + lane ;
308+ }
309+ return total ;
310+ }
311+
312+ function representativeJsonStringifyCycle ( jobs , lane ) {
313+ var total = 0 ;
314+ for ( var job = 0 ; job < jobs ; job = job + 1 ) {
315+ var threw = false ;
316+ try {
317+ JSON . stringify ( representativeJsonStringifyGraph ) ;
318+ } catch ( error ) {
319+ if ( ! ( error instanceof TypeError ) ) throw error ;
320+ threw = true ;
321+ }
322+ if ( ! threw ) throw new Error ( "JSON.stringify accepted an ancestor cycle" ) ;
323+ total = total + 1000000 + representativeJsonStringifyDepth + lane ;
324+ }
325+ return total ;
326+ }
327+
328+ function selectRepresentativeJsonStringifyShallow ( width ) {
329+ var siblings = [ ] ;
330+ var shared = { leaf : 1 } ;
331+ for ( var index = 0 ; index < width ; index = index + 1 )
332+ siblings . push ( shared ) ;
333+ representativeJsonStringifyGraph = siblings ;
334+ representativeJsonStringifyDepth = width ;
335+ representativeJsonStringifyExpectedLength = width * 11 + 1 ;
336+ representativeJsonStringifyExpectedLeafOffset = 2 ;
337+ return representativeJsonStringifyShallow ;
338+ }
339+
340+ function representativeJsonStringifyShallow ( jobs , lane ) {
341+ var total = 0 ;
342+ for ( var job = 0 ; job < jobs ; job = job + 1 ) {
343+ var encoded = JSON . stringify ( representativeJsonStringifyGraph ) ;
344+ var leafOffset = encoded . indexOf ( "\"leaf\":1" ) ;
345+ if ( encoded . length !== representativeJsonStringifyExpectedLength ||
346+ leafOffset !== representativeJsonStringifyExpectedLeafOffset ||
347+ encoded . charCodeAt ( 0 ) !== 91 || encoded . charCodeAt ( encoded . length - 1 ) !== 93 )
348+ throw new Error ( "JSON.stringify shallow witness drift" ) ;
349+ total = total + encoded . length + leafOffset +
350+ encoded . charCodeAt ( 0 ) + encoded . charCodeAt ( encoded . length - 1 ) + lane ;
351+ }
352+ return total ;
353+ }
354+
277355function representativeCollections ( jobs , lane , variant ) {
278356 var total = 0 ;
279357 for ( var job = 0 ; job < jobs ; job = job + 1 ) {
@@ -901,6 +979,12 @@ function benchmarkFunction(name) {
901979 if ( name === "representative_json_escaped_strings" ) return representativeJsonEscapedStrings ;
902980 if ( name === "representative_json_stringify_replacer" ) return representativeJsonStringifyReplacer ;
903981 if ( name === "representative_json_stringify_replacer_membership" ) return representativeJsonStringifyReplacerMembership ;
982+ if ( name === "representative_json_stringify_depth_512" ) return selectRepresentativeJsonStringifyDepth ( 512 , false ) ;
983+ if ( name === "representative_json_stringify_depth_1024" ) return selectRepresentativeJsonStringifyDepth ( 1024 , false ) ;
984+ if ( name === "representative_json_stringify_depth_2048" ) return selectRepresentativeJsonStringifyDepth ( 2048 , false ) ;
985+ if ( name === "representative_json_stringify_depth_4096" ) return selectRepresentativeJsonStringifyDepth ( 4096 , false ) ;
986+ if ( name === "representative_json_stringify_cycle_4096" ) return selectRepresentativeJsonStringifyDepth ( 4096 , true ) ;
987+ if ( name === "representative_json_stringify_shallow_4096" ) return selectRepresentativeJsonStringifyShallow ( 4096 ) ;
904988 if ( name === "representative_collections" ) return function ( jobs , lane ) { return representativeCollections ( jobs , lane , 0 ) ; } ;
905989 if ( name === "representative_collections_variant" ) return function ( jobs , lane ) { return representativeCollections ( jobs , lane , 1 ) ; } ;
906990 if ( name === "representative_strong_identity_collections" ) return function ( jobs , lane ) { return representativeStrongIdentityCollections ( jobs , lane , 0 ) ; } ;
0 commit comments