Created
November 16, 2025 13:07
-
-
Save almeidx/1a380bf430a4b330bde4e3b7f3bc2303 to your computer and use it in GitHub Desktop.
Memory usage comparison when storing a timestamp, between number, bigint, and string
This file contains hidden or 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
| 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); | |
| } |
This file contains hidden or 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
| 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; | |
| } |
This file contains hidden or 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
| 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); | |
| } |
This file contains hidden or 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
| 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