Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F2916284
bench_array_from.js
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
bench_array_from.js
View Options
const
ITERATIONS
=
1
_000_000
;
const
source
=
[
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
];
const
mapFn
=
x
=>
x
*
2
;
// --- Polyfill ---
const
ArrayFrom_polyfill
=
function
(
src
,
mapFn
,
thisArg
)
{
if
(
src
==
null
)
{
console
.
error
(
'TypeError: Cannot convert undefined or null to object'
);
return
[];
}
if
(
mapFn
!==
undefined
&&
typeof
mapFn
!==
'function'
)
{
console
.
error
(
'TypeError: mapFn is not a function'
);
return
[];
}
const
arr
=
[];
let
i
=
0
;
if
(
src
[
Symbol
.
iterator
]
!=
null
)
{
for
(
const
v
of
src
)
arr
.
push
(
mapFn
?
mapFn
.
call
(
thisArg
,
v
,
i
++
)
:
v
);
}
else
{
const
len
=
src
.
length
>>>
0
;
for
(;
i
<
len
;
i
++
)
arr
.
push
(
mapFn
?
mapFn
.
call
(
thisArg
,
src
[
i
],
i
)
:
src
[
i
]);
}
return
arr
;
};
// --- Builtin ---
function
benchBuiltin
()
{
const
start
=
performance
.
now
();
for
(
let
i
=
0
;
i
<
ITERATIONS
;
i
++
)
{
Array
.
from
(
source
,
mapFn
);
}
return
performance
.
now
()
-
start
;
}
// --- Polyfill ---
function
benchPolyfill
()
{
const
start
=
performance
.
now
();
for
(
let
i
=
0
;
i
<
ITERATIONS
;
i
++
)
{
ArrayFrom_polyfill
(
source
,
mapFn
);
}
return
performance
.
now
()
-
start
;
}
const
builtinMs
=
benchBuiltin
();
const
polyfillMs
=
benchPolyfill
();
console
.
log
(
`Array.from (builtin):
${
builtinMs
.
toFixed
(
2
)
}
ms`
);
console
.
log
(
`Array.from (polyfill):
${
polyfillMs
.
toFixed
(
2
)
}
ms`
);
console
.
log
(
`Ratio: builtin is
${
(
polyfillMs
/
builtinMs
).
toFixed
(
2
)
}
x
${
polyfillMs
>
builtinMs
?
'faster'
:
'slower'
}
than polyfill`
);
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Thu, Mar 26, 4:46 PM (1 d, 4 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
511713
Default Alt Text
bench_array_from.js (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment