11const fs = require ( "fs" ) ;
22
3- // Load WASM version
3+ // Load AssemblyScript version
44const nbodyAS = require ( "../assembly/index.js" ) ;
5- var nbodyRS ;
6- try {
7- nbodyRS = require ( "../rust/index.js" ) ;
8- } catch ( e ) { }
5+ // Load Rust/wasm version
6+ let nbodyRS ;
7+ try { nbodyRS = require ( "../rust/index.js" ) ; } catch ( e ) { }
98
109// Load JS version
11- var src = fs . readFileSync ( __dirname + "/../build/as_nbody.js" , "utf8" )
12- . replace ( / c o n s t r e t a s m F u n c [ ^ $ ] * $ / g, "" ) ;
13-
14- const nbodyAS_JS = eval ( src + ";asmFunc" ) ( {
15- Int8Array,
16- Int16Array,
17- Int32Array,
18- Uint8Array,
19- Uint16Array,
20- Uint32Array,
21- Float32Array,
22- Float64Array,
23- Math
24- } , {
25- abort : ( ) => { throw Error ( ) ; }
26- } , new ArrayBuffer ( 0x10000 ) ) ;
27-
28- // Load JS version
29- src = fs . readFileSync ( __dirname + "/../build/index.js" , "utf8" ) ;
30- const scopeJS = {
31- require : ( ) => { } ,
32- exports : { } ,
33- unchecked : expr => expr
34- } ;
35-
36- const nbodyJS = new Function (
37- ...Object . keys ( scopeJS ) . concat ( src + "\nreturn exports" ) ) ( ...Object . values ( scopeJS )
38- ) ;
10+ const nbodyJS = require ( "../build/index.js" ) ;
3911
4012function gcCollect ( ) {
4113 if ( global . gc ) {
@@ -58,45 +30,29 @@ function test(nbody, steps) {
5830 return t ;
5931}
6032
61- var steps = process . argv . length > 2 ? parseInt ( process . argv [ 2 ] , 10 ) : 20000000 ;
62-
63- function prologue ( name , steps ) {
64- console . log ( "Performing " + steps + " steps (" + name + ") ..." ) ;
65- }
33+ const steps = process . argv . length > 2
34+ ? parseInt ( process . argv [ 2 ] , 10 )
35+ : 20000000 ;
6636
67- function epilogue ( time ) {
68- console . log ( "Took " + ( time [ 0 ] * 1e3 + time [ 1 ] / 1e6 ) + "ms" ) ;
37+ function bench ( name , fn ) {
38+ console . log ( `Performing ${ steps } steps (${ name } ) ...` ) ;
39+ const time = test ( fn , steps ) ;
40+ console . log ( `Took ${ ( time [ 0 ] * 1e3 + time [ 1 ] / 1e6 ) . toFixed ( 0 ) } ms` ) ;
6941}
7042
7143console . log ( "\nCOLD SERIES:\n" ) ;
7244
73- prologue ( "AssemblyScript WASM" , steps ) ;
74- epilogue ( test ( nbodyAS , steps ) ) ;
75-
76- prologue ( "AssemblyScript JS" , steps ) ;
77- epilogue ( test ( nbodyAS_JS , steps ) ) ;
78-
79- prologue ( "JS" , steps ) ;
80- epilogue ( test ( nbodyJS , steps ) ) ;
81-
45+ bench ( "AssemblyScript WASM" , nbodyAS ) ;
46+ bench ( "JS" , nbodyJS ) ;
8247if ( nbodyRS ) {
83- prologue ( "Rust WASM" , steps ) ;
84- epilogue ( test ( nbodyRS , steps ) ) ;
48+ bench ( "Rust WASM" , nbodyRS ) ;
8549}
8650
8751console . log ( "\nWARMED UP SERIES:\n" ) ;
8852sleep ( 1000 ) ;
8953
90- prologue ( "AssemblyScript WASM" , steps ) ;
91- epilogue ( test ( nbodyAS , steps ) ) ;
92-
93- prologue ( "AssemblyScript JS" , steps ) ;
94- epilogue ( test ( nbodyAS_JS , steps ) ) ;
95-
96- prologue ( "JS" , steps ) ;
97- epilogue ( test ( nbodyJS , steps ) ) ;
98-
54+ bench ( "AssemblyScript WASM" , nbodyAS ) ;
55+ bench ( "JS" , nbodyJS ) ;
9956if ( nbodyRS ) {
100- prologue ( "Rust WASM" , steps ) ;
101- epilogue ( test ( nbodyRS , steps ) ) ;
57+ bench ( "Rust WASM" , nbodyRS ) ;
10258}
0 commit comments