Skip to content

Instantly share code, notes, and snippets.

@madeindjs

madeindjs/generator.ts

Last active Mar 10, 2020
Embed
What would you like to do?
class User {
public birthDate: Date;
public constructor(
public readonly firstname: string = 'Douggie',
public readonly lastname: string = 'Dougs',
) {
const birthYear = Math.floor((Math.random() * 50) + 1970)
this.birthDate = new Date(birthYear, 1, 1);
}
get age(): number {
return new Date().getFullYear() - this.birthDate.getFullYear()
}
}
function get10MUsers(): User[] {
const users: User[] = [];
for (let i = 0; i < 10_000_000 ; ++i) {
users.push(new User);
}
return users;
}
// for (const user of get10MUsers()) {
// console.log(`${user.firstname} ${user.lastname} has ${user.age}`);
// FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
// <--- Last few GCs --->
// oc[9121:0x103dd2000] 40912 ms: Mark-sweep 2048.9 (2053.4) -> 2048.4 (2054.1) MB, 2122.9 / 0.0 ms (+ 94.9 ms in 22 steps since start of marking, biggest step 5.4 ms, walltime since start of marking 2222 ms) (average mu = 0.098, current mu = 0.002) alloca[9121:0x103dd2000] 42972 ms: Mark-sweep 2049.7 (2054.1) -> 2049.3 (2054.9) MB, 1956.9 / 0.0 ms (+ 98.2 ms in 22 steps since start of marking, biggest step 5.3 ms, walltime since start of marking 2059 ms) (average mu = 0.052, current mu = 0.002) alloca
// }
function* get10MUsersGenerator(): Generator<User> {
for (let i = 0; i < 10_000_000 ; ++i) {
yield new User;
}
return;
}
// ts-node --compiler-options '{"downlevelIteration": true}' api/generator.t~
for (const user of get10MUsersGenerator()) {
console.log(`${user.firstname} ${user.lastname} has ${user.age}`);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.