diff --git a/playground/playground.ts b/playground/playground.ts index 1d5221b..927715f 100644 --- a/playground/playground.ts +++ b/playground/playground.ts @@ -5,6 +5,7 @@ import { withResolvers, yieldOrContinue, } from '../index' +import simulateWork from './utils/simulateWork' document.querySelector('#run-interactive')!.addEventListener('click', () => { run('interactive') @@ -88,54 +89,12 @@ async function runQueueTask(time: number = 1000) { } } -function simulateWork(): void { - // a 5x5 matrix - const matrixA = [ - [1, 2, 3, 4, 5], - [4, 5, 6, 7, 8], - [7, 8, 9, 10, 11], - [7, 8, 9, 10, 11], - [7, 8, 9, 10, 11], - ] - const matrixB = [ - [1, 2, 3, 4, 5], - [4, 5, 6, 7, 8], - [7, 8, 9, 10, 11], - [7, 8, 9, 10, 11], - [7, 8, 9, 10, 11], - ] - for (let i = 0; i < 5000; i++) { - matrixMultiplication(matrixA, matrixB) - } -} - -function matrixMultiplication(matrix1: number[][], matrix2: number[][]) { - const result = [] - const rows1 = matrix1.length - const cols1 = matrix1[0]!.length - const cols2 = matrix2[0]!.length - - for (let i = 0; i < rows1; i++) { - result[i] = [] - for (let j = 0; j < cols2; j++) { - // @ts-ignore - result[i][j] = 0 - for (let k = 0; k < cols1; k++) { - // @ts-ignore - result[i][j] += matrix1[i][k] * matrix2[k][j] - } - } - } - - return result -} - async function postTaskVsYieldOrContinue() { { const start = performance.now() let count = 0 while (performance.now() - start < 1000) { - await postTask() + await waitPostTask() count++ } console.log(count.toString(), '→ postTask()') @@ -160,7 +119,9 @@ async function postTaskVsYieldOrContinue() { } } -async function postTask(priority?: 'user-blocking' | 'user-visible' | 'background'): Promise { +async function waitPostTask( + priority?: 'user-blocking' | 'user-visible' | 'background', +): Promise { const { promise, resolve } = withResolvers() // @ts-ignore scheduler.postTask( diff --git a/playground/utils/simulateWork.ts b/playground/utils/simulateWork.ts new file mode 100644 index 0000000..b45713d --- /dev/null +++ b/playground/utils/simulateWork.ts @@ -0,0 +1,41 @@ +export default function simulateWork(): void { + // a 5x5 matrix + const matrixA = [ + [1, 2, 3, 4, 5], + [4, 5, 6, 7, 8], + [7, 8, 9, 10, 11], + [7, 8, 9, 10, 11], + [7, 8, 9, 10, 11], + ] + const matrixB = [ + [1, 2, 3, 4, 5], + [4, 5, 6, 7, 8], + [7, 8, 9, 10, 11], + [7, 8, 9, 10, 11], + [7, 8, 9, 10, 11], + ] + for (let i = 0; i < 5000; i++) { + matrixMultiplication(matrixA, matrixB) + } +} + +function matrixMultiplication(matrix1: number[][], matrix2: number[][]) { + const result = [] + const rows1 = matrix1.length + const cols1 = matrix1[0]!.length + const cols2 = matrix2[0]!.length + + for (let i = 0; i < rows1; i++) { + result[i] = [] + for (let j = 0; j < cols2; j++) { + // @ts-ignore + result[i][j] = 0 + for (let k = 0; k < cols1; k++) { + // @ts-ignore + result[i][j] += matrix1[i][k] * matrix2[k][j] + } + } + } + + return result +}