Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F7535553
class_vs_proto.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
class_vs_proto.js
View Options
var
SequenceProto
=
function
(
start
)
{
this
.
item
=
start
;
return
this
;
};
SequenceProto
.
prototype
.
next
=
function
()
{
var
temp
=
this
.
item
;
this
.
item
=
temp
+
2
;
return
temp
;
};
class
SequenceClass
{
constructor
(
start
)
{
this
.
item
=
start
;
}
next
()
{
const
temp
=
this
.
item
;
this
.
item
=
temp
+
2
;
return
temp
;
}
}
const
ITERATIONS
=
1
_000_000
;
const
WARMUP
=
100
_000
;
function
runProto
()
{
const
seq
=
new
SequenceProto
(
1
);
let
num
=
0
;
for
(
let
i
=
0
;
i
<
ITERATIONS
;
i
++
)
{
num
=
seq
.
next
();
}
return
num
;
}
function
runClass
()
{
const
seq
=
new
SequenceClass
(
1
);
let
num
=
0
;
for
(
let
i
=
0
;
i
<
ITERATIONS
;
i
++
)
{
num
=
seq
.
next
();
}
return
num
;
}
for
(
let
i
=
0
;
i
<
WARMUP
;
i
++
)
{
new
SequenceProto
(
1
).
next
();
new
SequenceClass
(
1
).
next
();
}
const
runs
=
10
;
const
protoTimes
=
[];
const
classTimes
=
[];
for
(
let
i
=
0
;
i
<
runs
;
i
++
)
{
const
t1
=
performance
.
now
();
runProto
();
protoTimes
.
push
(
performance
.
now
()
-
t1
);
const
t2
=
performance
.
now
();
runClass
();
classTimes
.
push
(
performance
.
now
()
-
t2
);
}
const
avg
=
arr
=>
arr
.
reduce
((
a
,
b
)
=>
a
+
b
,
0
)
/
arr
.
length
;
console
.
log
(
`Prototype avg:
${
avg
(
protoTimes
).
toFixed
(
3
)
}
ms`
);
console
.
log
(
`Class avg:
${
avg
(
classTimes
).
toFixed
(
3
)
}
ms`
);
console
.
log
(
`Difference:
${
Math
.
abs
(
avg
(
protoTimes
)
-
avg
(
classTimes
)).
toFixed
(
3
)
}
ms`
);
console
.
log
(
`Winner:
${
avg
(
protoTimes
)
<
avg
(
classTimes
)
?
'Prototype'
:
'Class'
}
`
);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Wed, Jun 17, 12:07 PM (1 d, 13 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
551304
Default Alt Text
class_vs_proto.js (1 KB)
Attached To
Mode
rANT Ant
Attached
Detach File
Event Timeline
Log In to Comment