Skip to content

Commit

Permalink
🐜 extract simulateWork()
Browse files Browse the repository at this point in the history
  • Loading branch information
astoilkov committed Feb 6, 2024
1 parent 8581190 commit 4f91e62
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 44 deletions.
49 changes: 5 additions & 44 deletions playground/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
withResolvers,
yieldOrContinue,
} from '../index'
import simulateWork from './utils/simulateWork'

document.querySelector('#run-interactive')!.addEventListener('click', () => {
run('interactive')
Expand Down Expand Up @@ -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()')
Expand All @@ -160,7 +119,9 @@ async function postTaskVsYieldOrContinue() {
}
}

async function postTask(priority?: 'user-blocking' | 'user-visible' | 'background'): Promise<void> {
async function waitPostTask(
priority?: 'user-blocking' | 'user-visible' | 'background',
): Promise<void> {
const { promise, resolve } = withResolvers()
// @ts-ignore
scheduler.postTask(
Expand Down
41 changes: 41 additions & 0 deletions playground/utils/simulateWork.ts
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 4f91e62

Please sign in to comment.