HashMap代码(这种实现方式是错误的,错误原因:代码中_map、_length变量是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代码(正确的实现):
/**
* 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
测试代码:
<!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