You've already forked json-iterator
							
							
				mirror of
				https://github.com/json-iterator/go.git
				synced 2025-10-31 00:07:40 +02:00 
			
		
		
		
	update README
This commit is contained in:
		
							
								
								
									
										119
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										119
									
								
								README.md
									
									
									
									
									
								
							| @@ -1,87 +1,61 @@ | |||||||
| # go | # json iterator (jsoniter) | ||||||
|  |  | ||||||
| faster than DOM, more usable than SAX/StAX | faster than DOM, more usable than SAX/StAX | ||||||
|  |  | ||||||
| # string | for performance numbers, see https://github.com/json-iterator/go-benchmark | ||||||
|  |  | ||||||
|  | # DOM style api | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| func Benchmark_jsoniter_string(b *testing.B) { | type StructOfTag struct { | ||||||
| 	for n := 0; n < b.N; n++ { | 	field1 string `json:"field-1"` | ||||||
| 		lexer := NewLexerWithArray([]byte(`"\ud83d\udc4a"`)) | 	field2 string `json:"-"` | ||||||
| 		lexer.LexString() | 	field3 int `json:",string"` | ||||||
|  | } | ||||||
|  |  | ||||||
|  | func Test_reflect_struct_tag_field(t *testing.T) { | ||||||
|  | 	iter := ParseString(`{"field-1": "hello", "field2": "", "field3": "100"}`) | ||||||
|  | 	struct_ := StructOfTag{field2: "world"} | ||||||
|  | 	iter.Read(&struct_) | ||||||
|  | 	if struct_.field1 != "hello" { | ||||||
|  | 		fmt.Println(iter.Error) | ||||||
|  | 		t.Fatal(struct_.field1) | ||||||
|  | 	} | ||||||
|  | 	if struct_.field2 != "world" { | ||||||
|  | 		fmt.Println(iter.Error) | ||||||
|  | 		t.Fatal(struct_.field2) | ||||||
|  | 	} | ||||||
|  | 	if struct_.field3 != 100 { | ||||||
|  | 		fmt.Println(iter.Error) | ||||||
|  | 		t.Fatal(struct_.field3) | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| 10000000	       140 ns/op | # StAX style api | ||||||
|  |  | ||||||
|  | Array | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| func Benchmark_json_string(b *testing.B) { | iter := ParseString(`[1,2,3]`) | ||||||
| 	for n := 0; n < b.N; n++ { | for iter.ReadArray() { | ||||||
| 		result := "" |  | ||||||
| 		json.Unmarshal([]byte(`"\ud83d\udc4a"`), &result) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| ```` |  | ||||||
|  |  | ||||||
| 2000000	       710 ns/op (5x slower) |  | ||||||
|  |  | ||||||
| # int |  | ||||||
|  |  | ||||||
| ``` |  | ||||||
| func Benchmark_jsoniter_int(b *testing.B) { |  | ||||||
| 	for n := 0; n < b.N; n++ { |  | ||||||
| 		lexer := NewLexerWithArray([]byte(`-100`)) |  | ||||||
| 		lexer.LexInt64() |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| ``` |  | ||||||
|  |  | ||||||
| 30000000	        60.1 ns/op |  | ||||||
|  |  | ||||||
| ``` |  | ||||||
| func Benchmark_json_int(b *testing.B) { |  | ||||||
| 	for n := 0; n < b.N; n++ { |  | ||||||
| 		result := int64(0) |  | ||||||
| 		json.Unmarshal([]byte(`-100`), &result) |  | ||||||
| 	} |  | ||||||
| } |  | ||||||
| ``` |  | ||||||
|  |  | ||||||
| 3000000	       505 ns/op (8x slower) |  | ||||||
|  |  | ||||||
| # array |  | ||||||
|  |  | ||||||
| ``` |  | ||||||
| func Benchmark_jsoniter_array(b *testing.B) { |  | ||||||
| 	for n := 0; n < b.N; n++ { |  | ||||||
| 		iter := ParseString(`[1,2,3]`) |  | ||||||
| 		for iter.ReadArray() { |  | ||||||
|   iter.ReadUint64() |   iter.ReadUint64() | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| 10000000	       189 ns/op | Object | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| func Benchmark_json_array(b *testing.B) { | type TestObj struct { | ||||||
| 	for n := 0; n < b.N; n++ { |     Field1 string | ||||||
| 		result := []interface{}{} |     Field2 uint64 | ||||||
| 		json.Unmarshal([]byte(`[1,2,3]`), &result) |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
| 1000000	      1327 ns/op |  | ||||||
|  |  | ||||||
| # object |  | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| func Benchmark_jsoniter_object(b *testing.B) { | iter := ParseString(`{"field1": "1", "field2": 2}`) | ||||||
| 	for n := 0; n < b.N; n++ { | obj := TestObj{} | ||||||
| 		iter := ParseString(`{"field1": "1", "field2": 2}`) | for field := iter.ReadObject(); field != ""; field = iter.ReadObject() { | ||||||
| 		obj := TestObj{} |  | ||||||
| 		for field := iter.ReadObject(); field != ""; field = iter.ReadObject() { |  | ||||||
|     switch field { |     switch field { | ||||||
|     case "field1": |     case "field1": | ||||||
|         obj.Field1 = iter.ReadString() |         obj.Field1 = iter.ReadString() | ||||||
| @@ -90,20 +64,17 @@ func Benchmark_jsoniter_object(b *testing.B) { | |||||||
|     default: |     default: | ||||||
|         iter.ReportError("bind object", "unexpected field") |         iter.ReportError("bind object", "unexpected field") | ||||||
|     } |     } | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| 5000000	       401 ns/op | Skip | ||||||
|  |  | ||||||
| ``` | ``` | ||||||
| func Benchmark_json_object(b *testing.B) { | iter := ParseString(`[ {"a" : [{"b": "c"}], "d": 102 }, "b"]`) | ||||||
| 	for n := 0; n < b.N; n++ { | iter.ReadArray() | ||||||
| 		result := TestObj{} | iter.Skip() | ||||||
| 		json.Unmarshal([]byte(`{"field1": "1", "field2": 2}`), &result) | iter.ReadArray() | ||||||
| 	} | if iter.ReadString() != "b" { | ||||||
|  |     t.FailNow() | ||||||
| } | } | ||||||
| ``` | ``` | ||||||
|  |  | ||||||
| 1000000	      1318 ns/op |  | ||||||
		Reference in New Issue
	
	Block a user