Skip to content

Instantly share code, notes, and snippets.

@almeidx
Created November 16, 2025 13:07
Show Gist options
  • Select an option

  • Save almeidx/1a380bf430a4b330bde4e3b7f3bc2303 to your computer and use it in GitHub Desktop.

Select an option

Save almeidx/1a380bf430a4b330bde4e3b7f3bc2303 to your computer and use it in GitHub Desktop.
Memory usage comparison when storing a timestamp, between number, bigint, and string
import {
reportMemoryUsage,
startMemoryReporting,
} from "./memory-report.mjs";
const initialMemoryUsage = startMemoryReporting();
const holder = [];
for (let i = 0; i < 1_000_000; ++i) {
const timestamp = 1499794027299000000n + BigInt(i);
holder.push(timestamp);
}
reportMemoryUsage(initialMemoryUsage);
// Prevent optimization
if (holder.length !== 1_000_000) {
console.debug(holder.length);
}
import process from "node:process";
export function startMemoryReporting() {
if (typeof globalThis.gc !== "function") {
console.warn(
"Garbage collector is not exposed. Run Node.js with --expose-gc to enable memory reporting."
);
process.exit(1);
}
globalThis.gc();
return process.memoryUsage();
}
/**
* @param {NodeJS.MemoryUsage} previous The previous memory usage report to compare against.
*/
export function reportMemoryUsage(previous) {
globalThis.gc();
const current = process.memoryUsage();
console.log("Relative Memory Usage Report:");
console.log(
` heapTotal: ${bytesToMB(current.heapTotal - previous.heapTotal)} MB`
);
console.log(
` heapUsed: ${bytesToMB(current.heapUsed - previous.heapUsed)} MB`
);
}
/**
* @param {number} bytes The number of bytes.
* @returns {number}
*/
function bytesToMB(bytes) {
return Math.round((bytes / 1024 / 1024) * 100) / 100;
}
import {
reportMemoryUsage,
startMemoryReporting,
} from "./memory-report.mjs";
const initialMemoryUsage = startMemoryReporting();
const holder = [];
for (let i = 0; i < 1_000_000; ++i) {
const timestamp = 1499794027299 + i;
holder.push(timestamp);
}
reportMemoryUsage(initialMemoryUsage);
// Prevent optimization
if (holder.length !== 1_000_000) {
console.debug(holder.length);
}
import {
reportMemoryUsage,
startMemoryReporting,
} from "./memory-report.mjs";
const initialMemoryUsage = startMemoryReporting();
const holder = [];
for (let i = 0; i < 1_000_000; ++i) {
const timestamp = `2017-07-11T17:27:07.${299000 + i}+00:00`;
holder.push(timestamp);
}
reportMemoryUsage(initialMemoryUsage);
// Prevent optimization
if (holder.length !== 1_000_000) {
console.debug(holder.length);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment