-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
2,000,101 additions
and
14 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,16 +1,50 @@ | ||
// @ts-check | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
class BenchmarkTests { | ||
loopSample() { | ||
console.log("\n==> Loop Sample") | ||
const start = Date.now(); | ||
let sum = 0; | ||
for (let i = 0; i < 1_000_000_000; i++) { | ||
sum += i; | ||
} | ||
const end = Date.now(); | ||
console.log(`Time: ${end - start} ms`) | ||
} | ||
/** | ||
* Time: 1227 ms | ||
*/ | ||
loopSample() { | ||
console.log('\n==> Loop Sample'); | ||
const start = Date.now(); | ||
let sum = 0; | ||
for (let i = 0; i < 1_000_000_000; i++) { | ||
sum += i; | ||
} | ||
const end = Date.now(); | ||
console.log(`Time: ${end - start} ms`); | ||
} | ||
|
||
/** | ||
* Sum: 49999995000000 | ||
* Time: 404 ms | ||
*/ | ||
memoryIntensiveSample() { | ||
console.log('\n==> Memory Intensive Sample'); | ||
const start = Date.now(); | ||
const numbers = []; | ||
for (let i = 0; i < 10_000_000; i++) { | ||
numbers.push(i); | ||
} | ||
const sum = numbers.reduce((acc, num) => acc + num, 0); | ||
const end = Date.now(); | ||
console.log('Sum:', sum); | ||
console.log('Time:', end - start, 'ms'); | ||
} | ||
|
||
/** | ||
* Time: 99ms | ||
*/ | ||
IOIntensiveTask() { | ||
console.log('\n==> IO Intensive Sample'); | ||
const filePath = path.join(__dirname, 'largeFileJS.txt'); | ||
const start = Date.now(); | ||
fs.writeFileSync(filePath, 'This is a benchmark test.\n'.repeat(1_000_000)); | ||
const content = fs.readFileSync(filePath, 'utf-8'); | ||
const end = Date.now(); | ||
console.log('Time: ', end - start, ' ms'); | ||
} | ||
} | ||
|
||
const benchmarkTests = new BenchmarkTests(); | ||
benchmarkTests.loopSample(); |
Oops, something went wrong.