-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbest-magic-data-rate-bench.js
44 lines (43 loc) · 1.47 KB
/
best-magic-data-rate-bench.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
var log = console.log
, dlength = 700
, stime = 0
, boundary = new Buffer( 57 )
// build test data to parse
, data = ( function () {
var test = new Buffer( dlength * 1024 * 1024),
t = 0,
tlen = test.length;
log( '- now building a %d GB test buffer..', ( dlength / 1024 ).toFixed( 2 ) );
for( ; ++t < tlen; ) {
test[ t ] = t % 256;
}
log( '- test buffer created..' );
return test;
} )()
// crook parse method
, magicParse = function ( pattern, data ) {
var i = 0,
dlen = data.length,
plen = pattern.length,
dchar = null,
pchar = pattern[ plen - 1 ];
log( '- now parsing test buffer with magic algorithm..' );
for( stime = Date.now(); i < dlen; i += plen ) {
/*
* access data and compare current byte with the last byte of pattern,
* then skip plen bytes, no other comparison.
* best performance O(n/m)
*/
if( pchar === data[ i ] ) {
// ..
}
}
return [];
}
, results = magicParse( boundary, data )
, duration = ( Date.now() - stime )
, datarate = ( ( dlength / duration ) * ( 7.8125 ) )
;
log( '- elapsed time is; %d millis (I\'m the best!)', duration );
log( '- datarate is: %d Gbit/sec (Magic!)', ( datarate ).toFixed( 2 ) );
log( '- results: 0 found (Sorry, I\'m a crook!)' );