Skip to content

Commit

Permalink
add: java vs js benchmark tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thutasann committed Dec 24, 2024
1 parent f18834f commit 33307b2
Show file tree
Hide file tree
Showing 4 changed files with 2,000,101 additions and 14 deletions.
59 changes: 56 additions & 3 deletions java_vs_javascript/benchmark_tests/BenchmarkTests.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
package java_vs_javascript.benchmark_tests;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class BenchmarkTests {

public static void main(String[] args) {
BenchmarkTests.loopSample();
public static void main(String[] args) throws IOException {
BenchmarkTests.IOIntensiveSample();
}

private static void loopSample() {
/**
* JAVA wins
* sum: 499999999500000000
* Time: 353.422166 ms
*/
public static void loopSample() {
System.out.println("\n==> Loop Sample");
long start = System.nanoTime();
long sum = 0;
Expand All @@ -17,4 +26,48 @@ private static void loopSample() {
System.out.println("sum: " + sum);
System.out.println("Time: " + (end - start) / 1e6 + " ms");
}

/**
* JAVA wins
* Sum: 49999995000000
* Time: 265.91675 ms
*/
public static void memoryIntensiveSample() {
System.out.println("\n==> Memory Intensive Sample");
long start = System.nanoTime();
List<Integer> numbers = new ArrayList<>();
for (int i = 0; i < 10_000_000; i++) {
numbers.add(i);
}
long sum = numbers.stream().mapToLong(Integer::longValue).sum();
long end = System.nanoTime();
System.out.println("Sum: " + sum);
System.out.println("Time: " + (end - start) / 1e6 + " ms");
}

/**
* Javascript wins
* Time: 169.921708 ms
*/
public static void IOIntensiveSample() throws IOException {
System.out.println("\n==> IO Intensive Sample");
File file = new File("largeFileJava.txt");
long start = System.nanoTime();

try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (int i = 0; i < 1_000_000; i++) {
writer.write("This is a benchmark test.\n");
}
}

try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
while (reader.readLine() != null) {
}
}

long end = System.nanoTime();
System.out.println("Time: " + (end - start) / 1e6 + " ms");

}

}
56 changes: 45 additions & 11 deletions java_vs_javascript/benchmark_tests/BenchmarkTests.js
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();
Loading

0 comments on commit 33307b2

Please sign in to comment.