title: How to optimize integer toString method with javascript?
body: <p>I want to use <code>Number.toString(62)</code> but ECMAScript does not support it (only support radix from 2 to 36), so I'm writing myself version of <code>Number.toString</code>.</p>
  
  <p>And I only need to use it to format <code>int64</code>, do not need to care about <code>double</code>.</p>
  
  <p>So, I benched the native methods for <code>Number</code> and <code>BigInt</code> with different <code>radix</code>.</p>
  
  <p>The bench code as follow:</p>
  
  <pre class="lang-js prettyprint-override"><code>import { Suite } from 'benchmark';
  import { shuffle } from 'lodash';
  
  const float = new Array(10000)
    .fill(0)
    .map(() =&gt; Math.random() * Number.MAX_SAFE_INTEGER);
  const int = float.map(Math.floor);
  const big = int.map(BigInt);
  
  export const suite = new Suite('native integer format')
    .add('Int64.toString(10)', () =&gt; shuffle(int).map((n) =&gt; n.toString(10)))
    .add('Int64.toString(16)', () =&gt; shuffle(int).map((n) =&gt; n.toString(16)))
    .add('Int64.toString(36)', () =&gt; shuffle(int).map((n) =&gt; n.toString(36)))
    .add('Float64.toString(10)', () =&gt; shuffle(float).map((n) =&gt; n.toString(10)))
    .add('Float64.toString(16)', () =&gt; shuffle(float).map((n) =&gt; n.toString(16)))
    .add('Float64.toString(36)', () =&gt; shuffle(float).map((n) =&gt; n.toString(36)))
    .add('BigInt.toString(10)', () =&gt; shuffle(big).map((n) =&gt; n.toString(10)))
    .add('BigInt.toString(16)', () =&gt; shuffle(big).map((n) =&gt; n.toString(16)))
    .add('BigInt.toString(36)', () =&gt; shuffle(big).map((n) =&gt; n.toString(36)))
    .add('Date.now()', () =&gt; shuffle(int).map(Date.now));
  </code></pre>
  
  <p>The result is:</p>
  
  <pre class="lang-sh prettyprint-override"><code>native integer format -&gt; Int64.toString(10) x 660 ops/sec ±2.82% (83 runs sampled)
  native integer format -&gt; Int64.toString(16) x 109 ops/sec ±0.66% (79 runs sampled)
  native integer format -&gt; Int64.toString(36) x 134 ops/sec ±0.42% (84 runs sampled)
  native integer format -&gt; Float64.toString(10) x 448 ops/sec ±2.49% (85 runs sampled)
  native integer format -&gt; Float64.toString(16) x 103 ops/sec ±1.15% (75 runs sampled)
  native integer format -&gt; Float64.toString(36) x 130 ops/sec ±1.36% (83 runs sampled)
  native integer format -&gt; BigInt.toString(10) x 424 ops/sec ±0.64% (92 runs sampled)
  native integer format -&gt; BigInt.toString(16) x 1,039 ops/sec ±1.86% (92 runs sampled)
  native integer format -&gt; BigInt.toString(36) x 532 ops/sec ±0.76% (92 runs sampled)
  native integer format -&gt; Date.now() x 1,163 ops/sec ±0.59% (93 runs sampled)
  </code></pre>
  
  <p>As you see it upon, the <code>BigInt.toString(16)</code> is the fastest one(except for Date.now, which is the object to reference).</p>
  
  <p>The performance of mine is a little big faster than <code>Int64.toString</code>.</p>
  
  <p>Is there any open source code or known algorithm to speed up it?</p>
  
  <p>And there is something interesting: the fastest one of <code>Number.toString(radix)</code> is <code>Number.toString(10)</code>, but it is <code>toString(16)</code> for <code>BigInt</code>. Is it a magic?</p>