index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 'use strict'
  2. const Benchmark = require('benchmark')
  3. const suite = new Benchmark.Suite
  4. function Complex() {
  5. this.bar = 'foo'
  6. this.foo = new Array(100).fill(0).map(e => Math.random())
  7. }
  8. Complex.prototype.method = function() {}
  9. const a = {b: 'hello', c: {foo: 'bar', bar: 'foo', error: new Error('Test')}, complex: new Complex()}
  10. a.a = a
  11. a.c.complex = a.complex
  12. a.env = process.env
  13. const fclone = require('../dist/fclone.js')
  14. const clone = require('clone')
  15. const deepcopy = require('deepcopy')
  16. const jsonstringifysafe = require('json-stringify-safe')
  17. const jsan = require('jsan')
  18. const circularjson = require('circular-json-es6')
  19. const util = require('util')
  20. suite
  21. .add('fclone', function() {
  22. let b = fclone(a)
  23. })
  24. .add('fclone + json.stringify', function() {
  25. let b = JSON.stringify(fclone(a))
  26. })
  27. .add('util.inspect (outputs a string)', function() {
  28. let b = util.inspect(a)
  29. })
  30. .add('jsan', function() {
  31. let b = jsan.stringify(a)
  32. })
  33. .add('circularjson', function() {
  34. let b = circularjson.stringify(a)
  35. })
  36. .add('deepcopy', function() {
  37. let b = deepcopy(a)
  38. })
  39. .add('json-stringify-safe', function() {
  40. let b = jsonstringifysafe(a)
  41. })
  42. .add('clone', function() {
  43. let b = clone(a)
  44. })
  45. .on('cycle', function(event) {
  46. console.log(String(event.target))
  47. })
  48. .on('complete', function() {
  49. console.log('Fastest is ' + this.filter('fastest').map('name'))
  50. })
  51. .run({ 'async': true })