1
1
import deepDiff from "deep-diff" ;
2
2
import deepObjectDiff from "deep-object-diff" ;
3
+ import { diffJson } from "diff" ;
3
4
import microdiff from "./dist/index.js" ;
5
+ import { hrtime } from "node:process" ;
6
+ import colors from "picocolors" ;
7
+
4
8
const obj = {
5
9
test : "test" ,
6
10
testing : true ,
@@ -13,12 +17,37 @@ const newObj = {
13
17
bananas : "awesome" ,
14
18
bestFruits : [ "bananas" , "kiwi" , "blueberries" , "blackberries" ] ,
15
19
} ;
16
- console . time ( "deep-diff" ) ;
17
- deepDiff . diff ( obj , newObj ) ;
18
- console . timeEnd ( "deep-diff" ) ;
19
- console . time ( "deep-object-diff" ) ;
20
- deepObjectDiff . detailedDiff ( obj , newObj ) ;
21
- console . timeEnd ( "deep-object-diff" ) ;
22
- console . time ( "microdiff" ) ;
23
- microdiff ( obj , newObj ) ;
24
- console . timeEnd ( "microdiff" ) ;
20
+ const benchmarks = {
21
+ "deep-diff" : ( ) => deepDiff . diff ( obj , newObj ) ,
22
+ "deep-object-diff" : ( ) => deepObjectDiff . detailedDiff ( obj , newObj ) ,
23
+ jsdiff : ( ) => diffJson ( obj , newObj ) ,
24
+ microdiff : ( ) => microdiff ( obj , newObj ) ,
25
+ } ;
26
+ let times = { } ;
27
+ for ( let benchmark in benchmarks ) {
28
+ times [ benchmark ] = [ ] ;
29
+ for ( let i = 1 ; i < 100 ; i ++ ) {
30
+ let time = hrtime ( ) ;
31
+ benchmarks [ benchmark ] ( ) ;
32
+ times [ benchmark ] . push ( hrtime ( time ) [ 1 ] ) ;
33
+ }
34
+ times [ benchmark ] =
35
+ times [ benchmark ] . reduce ( ( pv , nv ) => pv + nv ) / times [ benchmark ] . length ;
36
+ }
37
+ let output = [ ] ;
38
+ let fastest = "" ;
39
+ for ( let time in times ) {
40
+ if ( ! fastest || times [ time ] < times [ fastest ] ) {
41
+ fastest = time ;
42
+ }
43
+ }
44
+ for ( let time in times ) {
45
+ output . push (
46
+ `${ time } : ${ Math . round ( times [ time ] ) } ns - ${
47
+ fastest === time
48
+ ? colors . bold ( colors . green ( "Fastest" ) )
49
+ : `${ Math . round ( ( times [ time ] / times [ fastest ] - 1 ) * 100 ) } % slower`
50
+ } `
51
+ ) ;
52
+ }
53
+ console . log ( colors . bold ( colors . green ( "Benchmarks\n" ) ) + output . join ( "\n" ) ) ;
0 commit comments