jwt-go icon indicating copy to clipboard operation
jwt-go copied to clipboard

feat(json): replaced standard json by jsoniter

Open fredbi opened this issue 4 years ago • 2 comments

Signed-off-by: Frederic BIDON [email protected]

  • fixes #358
  • NOTE: breaking change with old go support (requires go1.7+)

Benchmarks

This section describe gains from migrating from encoding/json standard library to jsoniter (github.com/json-iterator/go).

TL;DR: pure JSON operations are about 30% faster, HS signing methods about 10% faster, whereas RSA signing methods remain dominated by cryptographic operations, making the impact of JSON improvements negligible.

Note that the parsing benchmark uses a small token with only a few claims: for large complex tokens, actual benefit is larger.

go version go1.12.6 linux/amd64, Intel Core [email protected] GHz (dual core)

$ go test -bench=. -benchmem -benchtime 10s

Signing benchmarks
HS algorithms - Standard encoding/json
BenchmarkHS256Signing-4   	10000000	      2458 ns/op	    1584 B/op	      32 allocs/op
BenchmarkHS384Signing-4   	 5000000	      2790 ns/op	    1968 B/op	      32 allocs/op
BenchmarkHS512Signing-4   	 5000000	      2799 ns/op	    2064 B/op	      32 allocs/op
HS algorithms - jsoniter
BenchmarkHS256Signing-4   	10000000	      2036 ns/op	    1308 B/op	      26 allocs/op
BenchmarkHS384Signing-4   	10000000	      2359 ns/op	    1693 B/op	      26 allocs/op
BenchmarkHS512Signing-4   	10000000	      2412 ns/op	    1789 B/op	      26 allocs/op
RS algorithms - Standard encoding/json
BenchmarkRS256Signing-4   	   20000	    926550 ns/op	   49217 B/op	     169 allocs/op
BenchmarkRS384Signing-4   	   20000	    907624 ns/op	   49329 B/op	     169 allocs/op
BenchmarkRS512Signing-4   	   20000	    941379 ns/op	   49346 B/op	     169 allocs/op
RS algorithms - jsoniter
BenchmarkRS256Signing-4   	   20000	    920077 ns/op	   48970 B/op	     164 allocs/op
BenchmarkRS384Signing-4   	   20000	    939164 ns/op	   49082 B/op	     164 allocs/op
BenchmarkRS512Signing-4   	   20000	    913491 ns/op	   49100 B/op	     164 allocs/op
Claims parsing benchmark

ParseUnverified applied on small token with a few custom claims.

standard json lib     BenchmarkParsing-4        	 2000000	      6556 ns/op	    4192 B/op	      94 allocs/op
jsoniter              BenchmarkParsing-4        	 3000000	      4886 ns/op	    3657 B/op	      91 allocs/op

fredbi avatar Sep 03 '19 12:09 fredbi

I would prefer to keep this library as free as possible of external dependencies. Version 4.0.0 introduces a method for you to drop in a different encoder for your own use.

dgrijalva avatar Jan 07 '20 01:01 dgrijalva

@dgrijalva. Sure. I didn't get that part. I'll wait for the new release, upgrade and pick my jsoniter. This PR will probably serve as a base for an example, I suppose.

fredbi avatar Feb 22 '20 18:02 fredbi