欢迎光临
我的个人博客网站

JS 实现 HashMap

HashMap代码(有问题的代码,有没有大神可以指出问题在哪?):

JS 实现 HashMapJS 实现 HashMap

/**  * HashMap  * 2021年09月09日  */  (function (global) {     var _map;     var _length;      global.HashMap = function () {         _map = {};         _length = 0;     };      global.HashMap.prototype = {         put: function (key, value) {             if (!_map.hasOwnProperty(key)) {                 _length++;             }             _map[key] = value;         },          get: function (key) {             if (_map.hasOwnProperty(key)) {                 return _map[key];             }             return null;         },          containsKey: function (key) {             return _map.hasOwnProperty(key);         },          size: function () {             return _length;         },          remove: function (key) {             if (_map.hasOwnProperty(key)) {                 _length--;                 return delete _map[key];             }             return false;         },          removeAll: function () {             _map = {};         },          keys: function () {             var keys = [];             for (var item in _map) {                 keys.push(item);             }             return keys;         },          values: function () {             var values = [];             for (var item in _map) {                 values.push(_map[item]);             }             return values;         }     };      global.HashMap.prototype.constructor = global.HashMap;  })(window);

View Code

HashMap代码:

JS 实现 HashMapJS 实现 HashMap

/**  * HashMap  * 2021年09月09日  */  HashMap = function () {     this._length = 0;     this._map = {}; };  HashMap.prototype = {     put: function (key, value) {         if (!this._map.hasOwnProperty(key)) {             this._length++;         }         this._map[key] = value;     },      get: function (key) {         if (this._map.hasOwnProperty(key)) {             return this._map[key];         }         return null;     },      containsKey: function (key) {         return this._map.hasOwnProperty(key);     },      size: function () {         return this._length;     },      remove: function (key) {         if (this._map.hasOwnProperty(key)) {             this._length--;             return delete this._map[key];         }         return false;     },      removeAll: function () {         this._length = 0;         this._map = {};     },      keys: function () {         var keys = [];         for (var item in this._map) {             keys.push(item);         }         return keys;     },      values: function () {         var values = [];         for (var item in this._map) {             values.push(this._map[item]);         }         return values;     } };  HashMap.prototype.constructor = HashMap;

View Code

测试代码:

JS 实现 HashMapJS 实现 HashMap

<!DOCTYPE html> <html> <head>     <title>HashMap测试</title>      <meta charset="utf-8" />     <meta http-equiv="X-UA-Compatible" content="IE=edge">     <meta name="viewport" content="width=device-width, initial-scale=1">      <style type="text/css">     </style>      <script type="text/javascript" src="jquery-1.7.1.js"></script>     <script type="text/javascript" src="hashmap.js"></script>  </head> <body>     <input type="button" value="测试1" onclick="testHashMap()" />     <input type="button" value="测试2" onclick="testArray()" />     <div id="div" style="height:800px;"></div>      <script type="text/javascript">         var n = 100000;         var div = $("#div");          //测试数据         debugger;         var list = [];         var map = new HashMap();         for (var i = 1; i <= n; i++) {             var val = Math.floor(Math.random() * n);             var key = "key" + val;              if (!map.containsKey(key)) {                 map.put(key, val);                 list.push({ key: key, value: val });             }         }         div.append("测试数据初始化完成,数据量:Array " + list.length + ",HashMap " + map.size() + "<br />");          //测试HashMap         function testHashMap() {             var t1 = new Date().getTime();              var sum = 0;             var count = 0;             for (var i = 20000; i <= 21000; i++) {                 var key = "key" + i;                  if (map.containsKey(key)) {                     var num = map.get(key);                     count++;                     sum += num;                 }             }              var t2 = new Date().getTime();              div.append("完成,结果:sum=" + sum + ",count=" + count + "耗时" + (t2 - t1).toString() + "毫秒<br />");         }          //测试Array         function testArray() {             var t1 = new Date().getTime();              var sum = 0;             var count = 0;             for (var i = 20000; i <= 21000; i++) {                 for (var k = 0; k < list.length; k++) {                     var key = "key" + i;                     var item = list[k];                      if (item.key == key) {                         count++;                         sum += item.value;                     }                 }             }              var t2 = new Date().getTime();              div.append("完成,结果:sum=" + sum + ",count=" + count + "耗时" + (t2 - t1).toString() + "毫秒<br />");         }      </script> </body> </html>

View Code

 

赞(0) 打赏
未经允许不得转载:张拓的天空 » JS 实现 HashMap
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

专业的IT技术经验分享 更专业 更方便

联系我们本站主机

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏