@@ -4,12 +4,12 @@ const { |
4 | 4 | ArrayFrom, |
5 | 5 | ArrayIsArray, |
6 | 6 | ArrayPrototypeFilter, |
7 | | - ArrayPrototypeFlatMap, |
8 | 7 | ArrayPrototypeIncludes, |
9 | 8 | ArrayPrototypePush, |
10 | 9 | ArrayPrototypePushApply, |
11 | 10 | ArrayPrototypeSlice, |
12 | 11 | ArrayPrototypeSort, |
| 12 | + ArrayPrototypeConcat, |
13 | 13 | Error, |
14 | 14 | ObjectDefineProperties, |
15 | 15 | ObjectFreeze, |
@@ -34,7 +34,6 @@ const { |
34 | 34 | const { |
35 | 35 | InternalPerformanceEntry, |
36 | 36 | isPerformanceEntry, |
37 | | - kBufferNext, |
38 | 37 | } = require('internal/perf/performance_entry'); |
39 | 38 |
|
40 | 39 | const { |
@@ -89,8 +88,8 @@ const kSupportedEntryTypes = ObjectFreeze([ |
89 | 88 | ]); |
90 | 89 |
|
91 | 90 | // Performance timeline entry Buffers |
92 | | -const markEntryBuffer = createBuffer(); |
93 | | -const measureEntryBuffer = createBuffer(); |
| 91 | +let markEntryBuffer = []; |
| 92 | +let measureEntryBuffer = []; |
94 | 93 | const kMaxPerformanceEntryBuffers = 1e6; |
95 | 94 | const kClearPerformanceEntryBuffers = ObjectFreeze({ |
96 | 95 | 'mark': 'performance.clearMarks', |
@@ -154,9 +153,7 @@ function maybeIncrementObserverCount(type) { |
154 | 153 | class PerformanceObserverEntryList { |
155 | 154 | constructor(entries) { |
156 | 155 | this[kBuffer] = ArrayPrototypeSort(entries, (first, second) => { |
157 | | - if (first.startTime < second.startTime) return -1; |
158 | | - if (first.startTime > second.startTime) return 1; |
159 | | - return 0; |
| 156 | + return first.startTime - second.startTime; |
160 | 157 | }); |
161 | 158 | } |
162 | 159 |
|
@@ -344,15 +341,8 @@ function enqueue(entry) { |
344 | 341 | return; |
345 | 342 | } |
346 | 343 |
|
347 | | - const count = buffer.count + 1; |
348 | | - buffer.count = count; |
349 | | - if (count === 1) { |
350 | | - buffer.head = entry; |
351 | | - buffer.tail = entry; |
352 | | - return; |
353 | | - } |
354 | | - buffer.tail[kBufferNext] = entry; |
355 | | - buffer.tail = entry; |
| 344 | + ArrayPrototypePush(buffer, entry); |
| 345 | + const count = buffer.length; |
356 | 346 |
|
357 | 347 | if (count > kMaxPerformanceEntryBuffers && |
358 | 348 | !kWarnedEntryTypes.has(entryType)) { |
@@ -372,63 +362,40 @@ function enqueue(entry) { |
372 | 362 | } |
373 | 363 |
|
374 | 364 | function clearEntriesFromBuffer(type, name) { |
375 | | - let buffer; |
376 | | - if (type === 'mark') { |
377 | | - buffer = markEntryBuffer; |
378 | | - } else if (type === 'measure') { |
379 | | - buffer = measureEntryBuffer; |
380 | | - } else { |
381 | | - return; |
382 | | - } |
383 | | - if (name === undefined) { |
384 | | - resetBuffer(buffer); |
| 365 | + if (type !== 'mark' && type !== 'measure') { |
385 | 366 | return; |
386 | 367 | } |
387 | 368 |
|
388 | | - let head = null; |
389 | | - let tail = null; |
390 | | - let count = 0; |
391 | | - for (let entry = buffer.head; entry !== null; entry = entry[kBufferNext]) { |
392 | | - if (entry.name !== name) { |
393 | | - head = head ?? entry; |
394 | | - tail = entry; |
395 | | - continue; |
396 | | - } |
397 | | - if (tail === null) { |
398 | | - continue; |
399 | | - } |
400 | | - tail[kBufferNext] = entry[kBufferNext]; |
401 | | - count++; |
| 369 | + if (type === 'mark') { |
| 370 | + markEntryBuffer = name === undefined ? |
| 371 | + [] : ArrayPrototypeFilter(markEntryBuffer, (entry) => entry.name !== name); |
| 372 | + } else { |
| 373 | + measureEntryBuffer = name === undefined ? |
| 374 | + [] : ArrayPrototypeFilter(measureEntryBuffer, (entry) => entry.name !== name); |
402 | 375 | } |
403 | | - buffer.head = head; |
404 | | - buffer.tail = tail; |
405 | | - buffer.count = count; |
406 | 376 | } |
407 | 377 |
|
408 | 378 | function filterBufferMapByNameAndType(name, type) { |
409 | 379 | let bufferList; |
410 | 380 | if (type === 'mark') { |
411 | | - bufferList = [markEntryBuffer]; |
| 381 | + bufferList = markEntryBuffer; |
412 | 382 | } else if (type === 'measure') { |
413 | | - bufferList = [measureEntryBuffer]; |
| 383 | + bufferList = measureEntryBuffer; |
414 | 384 | } else if (type !== undefined) { |
415 | 385 | // Unrecognized type; |
416 | 386 | return []; |
417 | 387 | } else { |
418 | | - bufferList = [markEntryBuffer, measureEntryBuffer]; |
| 388 | + bufferList = ArrayPrototypeConcat(markEntryBuffer, measureEntryBuffer); |
419 | 389 | } |
420 | | - return ArrayPrototypeFlatMap(bufferList, |
421 | | - (buffer) => filterBufferByName(buffer, name)); |
422 | | -} |
423 | | - |
424 | | -function filterBufferByName(buffer, name) { |
425 | | - const arr = []; |
426 | | - for (let entry = buffer.head; entry !== null; entry = entry[kBufferNext]) { |
427 | | - if (name === undefined || entry.name === name) { |
428 | | - ArrayPrototypePush(arr, entry); |
429 | | - } |
| 390 | + if (name !== undefined) { |
| 391 | + bufferList = ArrayPrototypeFilter(bufferList, (buffer) => buffer.name === name); |
| 392 | + } else if (type !== undefined) { |
| 393 | + bufferList = ArrayPrototypeSlice(bufferList); |
430 | 394 | } |
431 | | - return arr; |
| 395 | + |
| 396 | + return ArrayPrototypeSort(bufferList, (first, second) => { |
| 397 | + return first.startTime - second.startTime; |
| 398 | + }); |
432 | 399 | } |
433 | 400 |
|
434 | 401 | function observerCallback(name, type, startTime, duration, details) { |
@@ -476,20 +443,6 @@ function hasObserver(type) { |
476 | 443 | return observerCounts[observerType] > 0; |
477 | 444 | } |
478 | 445 |
|
479 | | -function createBuffer() { |
480 | | - return { |
481 | | - head: null, |
482 | | - tail: null, |
483 | | - count: 0, |
484 | | - }; |
485 | | -} |
486 | | - |
487 | | -function resetBuffer(buffer) { |
488 | | - buffer.head = null; |
489 | | - buffer.tail = null; |
490 | | - buffer.count = 0; |
491 | | -} |
492 | | - |
493 | 446 | module.exports = { |
494 | 447 | PerformanceObserver, |
495 | 448 | PerformanceObserverEntryList, |
|