Object Keys Hoist Integer-Like Strings First

Object.keys sorts integer-like keys ascending before insertion-ordered string keys, while Map keeps pure insertion order — prefix numbered keys to opt out.

JavascriptData Integrity

Code

javascriptsnippet
24 lines
const settings = {
  theme: "dark",
  2: "two",
  0: "zero",
  beta: true,
  1: "one",
};

// 整数样式的键升序排在最前,其余键保持插入顺序
console.log(Object.keys(settings));

// Map 不做整数重排:顺序就是插入顺序
const ordered = new Map([
  ["theme", "dark"],
  ["2", "two"],
  ["0", "zero"],
  ["beta", true],
  ["1", "one"],
]);
console.log([...ordered.keys()]);

// 带编号的配置想保插入顺序:键里加非数字前缀即可
const stages = { "s10:build": 1, "s2:lint": 2, "s7:test": 3 };
console.log(Object.keys(stages));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

[ '0', '1', '2', 'theme', 'beta' ]
[ 'theme', '2', '0', 'beta', '1' ]
[ 's10:build', 's2:lint', 's7:test' ]

How it works

Top-level bindings settings, ordered, stages hold the demo state — nothing sits inside a function scope.

The top-level object literal pins theme, beta as plain values — no derivation, no imports behind them.

What to watch for

  • Ready as-is on a bare install of the language.

Statements that matter

const ordered = new Map([
  ["theme", "dark"],
  ["2", "two"],
  ["0", "zero"],
  ["beta", true],
  ["1", "one"],
]);

Interactive Tools for This Topic

FAQ

Is a package install part of the setup?
No. Installation is unnecessary because only built-in APIs are used. Its state lives in module-level bindings: `settings`, `ordered`, `stages`.