-
-
Notifications
You must be signed in to change notification settings - Fork 169
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(readability): add document-write benchmark
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
packages/metascraper-readability/benchmark/document-write.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'use strict' | ||
|
||
const { Window } = require('happy-dom') | ||
const { readFileSync } = require('fs') | ||
|
||
const url = 'https://arxiv.org/pdf/2412.06592' | ||
const html = readFileSync('./fixture.html', 'utf8') | ||
|
||
const isEqual = (value1, value2) => | ||
JSON.stringify(value1) === JSON.stringify(value2) | ||
|
||
const cases = { | ||
'document.documentElement.innerHTML': () => { | ||
const window = new Window({ url }) | ||
const document = window.document | ||
document.documentElement.innerHTML = html | ||
return document | ||
}, | ||
'document.write': async () => { | ||
const window = new Window({ url }) | ||
const document = window.document | ||
document.write(html) | ||
await window.happyDOM.waitUntilComplete() | ||
return document | ||
} | ||
} | ||
|
||
const measure = async fn => { | ||
const now = Date.now() | ||
const result = await fn() | ||
return { result, duration: Date.now() - now } | ||
} | ||
|
||
const iterations = 10 | ||
|
||
;(async () => { | ||
const results = await Promise.all( | ||
Object.entries(cases).map(async ([name, fn]) => { | ||
let totalDuration = 0 | ||
let result | ||
for (let i = 0; i < iterations; i++) { | ||
const { result: output, duration } = await measure(fn) | ||
totalDuration += duration | ||
if (i === iterations - 1) result = output | ||
} | ||
const averageDuration = totalDuration / iterations | ||
return { name, duration: averageDuration, result } | ||
}) | ||
) | ||
|
||
const [firstResult, ...otherResults] = results.map(({ result }) => result) | ||
|
||
for (const result of otherResults) { | ||
if (!isEqual(firstResult, result)) { | ||
throw new Error('Results are different') | ||
} | ||
} | ||
|
||
results.forEach(({ name, duration }) => { | ||
console.log(`${name}: ${duration}ms`) | ||
}) | ||
})() |