Created
May 2, 2016 06:22
-
-
Save fengmk2/e6bb787fbe0d13de78fc73f29ab45c5d to your computer and use it in GitHub Desktop.
Find out the faster way to create a simple map object https://cnodejs.org/topic/571e0c445a26c4a841ecbcf1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * Copyright(c) node-modules and other contributors. | |
| * MIT Licensed | |
| * | |
| * Authors: | |
| * fengmk2 <fengmk2@gmail.com> (http://fengmk2.com) | |
| */ | |
| 'use strict'; | |
| /** | |
| * Module dependencies. | |
| */ | |
| var crypto = require('crypto'); | |
| var utility = require('../'); | |
| var Benchmark = require('benchmark'); | |
| var benchmarks = require('beautify-benchmark'); | |
| var suite = new Benchmark.Suite(); | |
| function map0(obj) { | |
| var map = {}; | |
| if (!obj) { | |
| return map; | |
| } | |
| for (var key in obj) { | |
| map[key] = obj[key]; | |
| } | |
| return map; | |
| } | |
| function map1(obj) { | |
| var map = Object.create(null); | |
| if (!obj) { | |
| return map; | |
| } | |
| for (var key in obj) { | |
| map[key] = obj[key]; | |
| } | |
| return map; | |
| } | |
| function map2(obj) { | |
| var map = new Map(); | |
| if (!obj) { | |
| return map; | |
| } | |
| for (var key in obj) { | |
| map.set(key, obj[key]); | |
| } | |
| return map; | |
| } | |
| function Store() {} | |
| Store.prototype = Object.create(null); | |
| function map3(obj) { | |
| var map = new Store(); | |
| if (!obj) { | |
| return map; | |
| } | |
| for (var key in obj) { | |
| map[key] = obj[key]; | |
| } | |
| return map; | |
| } | |
| console.log('map0: %j', map0({ foo: 'bar' })); | |
| console.log('map1: %j', map1({ foo: 'bar' })); | |
| console.log('map3: %j', map3({ foo: 'bar' })); | |
| suite | |
| .add('map = new Store()', function() { | |
| map3({ foo: 'bar' }); | |
| }) | |
| .add('map = {}', function() { | |
| map0({ foo: 'bar' }); | |
| }) | |
| .add('map = Object.create(null)', function() { | |
| map1({ foo: 'bar' }); | |
| }) | |
| .add('map = new Map()', function() { | |
| map2({ foo: 'bar' }); | |
| }) | |
| .on('cycle', function(event) { | |
| benchmarks.add(event.target); | |
| }) | |
| .on('start', function(event) { | |
| console.log('\n map Benchmark\n node version: %s, date: %s\n Starting...', | |
| process.version, Date()); | |
| }) | |
| .on('complete', function done() { | |
| benchmarks.log(); | |
| }) | |
| .run({ 'async': false }); | |
| // node benchmark/map.js | |
| // | |
| // map0: {"foo":"bar"} | |
| // map1: {"foo":"bar"} | |
| // map3: {"foo":"bar"} | |
| // | |
| // map Benchmark | |
| // node version: v4.4.3, date: Mon May 02 2016 14:16:53 GMT+0800 (CST) | |
| // Starting... | |
| // 4 tests completed. | |
| // | |
| // map = new Store() x 28,292,183 ops/sec ±0.60% (98 runs sampled) | |
| // map = {} x 26,357,941 ops/sec ±0.64% (92 runs sampled) | |
| // map = Object.create(null) x 6,442,737 ops/sec ±0.60% (98 runs sampled) | |
| // map = new Map() x 4,299,817 ops/sec ±0.57% (99 runs sampled) | |
| // | |
| // node benchmark/map.js | |
| // | |
| // map0: {"foo":"bar"} | |
| // map1: {"foo":"bar"} | |
| // map3: {"foo":"bar"} | |
| // | |
| // map Benchmark | |
| // node version: v6.0.0, date: Mon May 02 2016 14:17:27 GMT+0800 (CST) | |
| // Starting... | |
| // 4 tests completed. | |
| // | |
| // map = new Store() x 38,653,790 ops/sec ±0.54% (99 runs sampled) | |
| // map = {} x 39,003,607 ops/sec ±0.70% (95 runs sampled) | |
| // map = Object.create(null) x 9,407,616 ops/sec ±0.54% (98 runs sampled) | |
| // map = new Map() x 6,951,186 ops/sec ±0.60% (96 runs sampled) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
你的测试有问题的, 测试成绩包括了对象创建的损耗
而不是实际的 get set 等性能
dic 实际应用场景中, 创建很少