当前位置: 首页 > article >正文

是否可以将缓存的 hashCode 方法添加到原始字符串?

问题描述

我有一些依赖于哈希码的方法,比如JS中的自定义唯一和哈希集合。

由于JS中的字符串是不可变的,并且哈希码是昂贵的操作,因此缓存它是有意义的,但不幸的是,无法在字符串上设置属性,因此下面的代码将不起作用。

我想知道是否有某种方法可以使其工作,也许是一些隐藏的内置foo.builtInHashCode()函数?

function calculateStringHashCode(s) {
  console.log("Calculating hash code for", s)
  return s.length
}

String.prototype.hashCode = function() {
  return this.cachedHashCode ??= calculateStringHashCode(this)
}

const s = "foo"
console.log(s.hashCode())
console.log(s.hashCode())

更新,或者可能使用外部缓存,类似于下面的非工作代码,但不幸的是WeakMap不接受原语作为键。

declare global {
  interface String {
    hashCode(): number
  }
}

const hashes = new WeakMap<string, number>() // <= Error primitives can't be keys
String.prototype.hashCode = function (this: string): number {
  let hash = hashes.get(this)
  if (hash == undefined) {
    console.log('calculating hash')
    hash = this.length // complex calculation of hash
    hashes.set(this, hash)
  }
  return hash
}

console.log('hi'.hashCode())
console.log('hi'.hashCode())

解决方案

问题不在于字符串的不可变性,而在于原型的工作方式。我建议使用这种变体:

function calculateStringHashCode(s) {
  console.log("Calculating hash code for", s)
  return s.length
}

String.prototype.cachedHashCodes = { }

String.prototype.hashCode = function() {
  return this.cachedHashCodes[this] ??=calculateStringHashCode(this)
}

const s = "foo"
const t = "bar"
console.log(s.hashCode())
console.log(s.hashCode())
console.log(t.hashCode())
console.log(s.hashCode())
console.log(t.hashCode())

这个想法是在原型中存储一个对哈希对象的不可变引用,并为每个字符串添加值。


http://www.kler.cn/news/340984.html

相关文章:

  • 深度学习模型
  • LabelImag标注工具环境配置
  • 【Matlab案例】imageJ + matlab 实现物体轨迹追踪及路径彩色上色
  • 【数据分析】参数检验与非参数检验
  • C语言 | Leetcode C语言题解之第468题验证IP地址
  • 【python 简易入门应用教程】第一部分:Python 基础
  • 信息安全工程师(40)防火墙技术应用
  • Word 首行缩进 2 字符怎么设置?具体步骤演示
  • html复习
  • 内容营销:基于大模型的内容再利用
  • 循环神经网络-LSTM网络
  • protobuf之Message
  • VirtualBox+Vagrant快速搭建Centos7系统【最新详细教程】
  • 库函数相关(上一篇补充)
  • WPF中的Window类
  • 【hot100-java】N 皇后
  • JavaScript 网页设计案例与技巧
  • Linux入门3——vim的简单使用
  • GEE 教程:利用Landsat函数计算不同缓冲区内的NDVI,NDWI和EVI的平均值
  • Django makemigrations时出现TypeError: ‘module‘ object is not iterable