You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					40 lines
				
				930 B
			
		
		
			
		
	
	
					40 lines
				
				930 B
			| 
								 
											3 years ago
										 
									 | 
							
								'use strict'
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const Benchmark = require('benchmark')
							 | 
						||
| 
								 | 
							
								const sjson = require('..')
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const internals = {
							 | 
						||
| 
								 | 
							
								  text: '{ "a": 5, "b": 6, "__proto__": { "x": 7 }, "c": { "d": 0, "e": "text", "__proto__": { "y": 8 }, "f": { "g": 2 } } }'
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								const suite = new Benchmark.Suite()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								suite
							 | 
						||
| 
								 | 
							
								  .add('JSON.parse', () => {
							 | 
						||
| 
								 | 
							
								    JSON.parse(internals.text)
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .add('secure-json-parse parse', () => {
							 | 
						||
| 
								 | 
							
								    sjson.parse(internals.text, { protoAction: 'remove' })
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .add('secure-json-parse safeParse', () => {
							 | 
						||
| 
								 | 
							
								    sjson.safeParse(internals.text)
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .add('reviver', () => {
							 | 
						||
| 
								 | 
							
								    JSON.parse(internals.text, internals.reviver)
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .on('cycle', (event) => {
							 | 
						||
| 
								 | 
							
								    console.log(String(event.target))
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .on('complete', function () {
							 | 
						||
| 
								 | 
							
								    console.log('Fastest is ' + this.filter('fastest').map('name'))
							 | 
						||
| 
								 | 
							
								  })
							 | 
						||
| 
								 | 
							
								  .run({ async: true })
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								internals.reviver = function (key, value) {
							 | 
						||
| 
								 | 
							
								  if (key === '__proto__') {
							 | 
						||
| 
								 | 
							
								    return undefined
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  return value
							 | 
						||
| 
								 | 
							
								}
							 |