Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F9917004
bench_prototype_chain.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
bench_prototype_chain.js
View Options
function
nowMs
()
{
return
Date
.
now
();
}
function
makeChain
(
depth
)
{
function
C
()
{}
const
root
=
{
marker
:
1
};
C
.
prototype
=
root
;
let
obj
=
Object
.
create
(
root
);
for
(
let
i
=
0
;
i
<
depth
;
i
++
)
obj
=
Object
.
create
(
obj
);
return
{
C
,
obj
,
root
};
}
function
bench
(
label
,
fn
,
iters
)
{
const
t0
=
nowMs
();
const
result
=
fn
(
iters
);
const
dt
=
nowMs
()
-
t0
;
const
opsPerMs
=
dt
>
0
?
(
iters
/
dt
).
toFixed
(
2
)
:
'inf'
;
console
.
log
(
label
+
': '
+
dt
+
'ms ('
+
opsPerMs
+
' ops/ms) result='
+
result
);
}
function
runDepth
(
depth
)
{
const
iters
=
2
_000_000
;
const
{
C
,
obj
,
root
}
=
makeChain
(
depth
);
console
.
log
(
'\n=== depth='
+
depth
+
' ==='
);
console
.
log
(
'sanity instanceof='
+
(
obj
instanceof
C
));
console
.
log
(
'sanity isPrototypeOf='
+
root
.
isPrototypeOf
(
obj
));
console
.
log
(
'sanity marker='
+
obj
.
marker
);
bench
(
'instanceof'
,
n
=>
{
let
c
=
0
;
for
(
let
i
=
0
;
i
<
n
;
i
++
)
if
(
obj
instanceof
C
)
c
++
;
return
c
;
},
iters
);
bench
(
'isPrototypeOf'
,
n
=>
{
let
c
=
0
;
for
(
let
i
=
0
;
i
<
n
;
i
++
)
if
(
root
.
isPrototypeOf
(
obj
))
c
++
;
return
c
;
},
iters
);
bench
(
'prop_lookup'
,
n
=>
{
let
c
=
0
;
for
(
let
i
=
0
;
i
<
n
;
i
++
)
c
+=
obj
.
marker
;
return
c
;
},
iters
);
}
console
.
log
(
'prototype-chain benchmark'
);
runDepth
(
8
);
runDepth
(
24
);
runDepth
(
40
);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Sat, Aug 1, 8:10 PM (10 h, 39 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
587505
Default Alt Text
bench_prototype_chain.js (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment