Skip to content

Commit f32fc31

Browse files
committed
feat: pre-allocating array
1 parent d61861c commit f32fc31

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

packages/core-js/modules/web.atob.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ var getBuiltIn = require('../internals/get-built-in');
55
var uncurryThis = require('../internals/function-uncurry-this');
66
var call = require('../internals/function-call');
77
var fails = require('../internals/fails');
8+
var from = require('../internals/array-from');
89
var toString = require('../internals/to-string');
910
var validateArgumentsLength = require('../internals/validate-arguments-length');
1011
var c2i = require('../internals/base64-map').c2i;
@@ -47,7 +48,8 @@ $({ global: true, bind: true, enumerable: true, forced: FORCED }, {
4748
// `webpack` dev server bug on IE global methods - use call(fn, global, ...)
4849
if (BASIC && !NO_SPACES_IGNORE && !NO_ENCODING_CHECK) return call($atob, globalThis, data);
4950
var string = replace(toString(data), whitespaces, '');
50-
var output = [];
51+
// every 4 characters produce 3 bytes, so ceil(length * 3 / 4) is the number of bytes
52+
var output = Array.from({ length: Math.ceil(string.length * 3 / 4) });
5153
var position = 0;
5254
var bc = 0;
5355
var length, chr, bs;

packages/core-js/modules/web.btoa.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ $({ global: true, bind: true, enumerable: true, forced: !BASIC || NO_ARG_RECEIVI
3535
// `webpack` dev server bug on IE global methods - use call(fn, global, ...)
3636
if (BASIC) return call($btoa, globalThis, toString(data));
3737
var string = toString(data);
38-
var output = [];
38+
// every 3 characters produce 4 bytes, so ceil(length * 4 / 3) is the number of bytes
39+
var output = Array.from({ length: Math.ceil(string.length * 4 / 3) });
3940
var position = 0;
4041
var map = i2c;
4142
var block, charCode;

0 commit comments

Comments
 (0)