是 javascript 中的静态变量 "thread safe"
are static variables "thread safe" in javascript
我正在制作一个 node.js 网络服务器来处理大负载、计算和副本,例如我需要一个大对象的深层副本:
const largeObject = { bla: "bla" } // ...
class Example {
constructor() {
this.copy = JSON.loads(JSON.stringify(largeObject))
this.copy.bla = "blo" // in reality the changes will different per request
}
doStuff(args) {
// do stuff with the deep copy
}
}
现在这工作正常,对于每个请求上下文,我都可以创建 1 个新的深层副本并在 class 中使用它。但是我的 class 变得越来越大而且没有结构,所以我想把它们分成不同的 classes。我想过用静态深拷贝实现一个基础 class,这样每个请求我都可以更改基础 class 上的副本并在我的另一个 class 中实现 class =]es.
const largeObject = { bla: "bla" } // ...
class Example {
static copy;
constructor() {
Example.copy = JSON.loads(JSON.stringify(largeObject))
Example.copy.bla = "blo" // in reality the changes will different per request
}
}
class DoWork {
constructor(someValue) {
this.someValue = someValue
}
doStuff(args) {
// do stuff Example.copy
}
}
出于性能原因,我只想在每个请求中深度复制对象一次,没有理由在每次 class 初始化时都深度复制对象。但是我害怕使用技术上比请求上下文更长寿的“全局”变量,我会遇到竞争条件和重叠上下文的问题。这是一个真正的问题还是 node.js 的单线程环境是否足够安全来处理这个问题。
该代码不会 运行 进入线程问题,不会。
Node.js 不是单线程的,但除非您通过 workers module, your code runs on a single thread, and even if you do create worker threads those threads run isolated from one another and from the main thread (they don't share a global environment; but they can communicate via messsaging and share memory in a very specific, bounded way via SharedArrayBuffer
).
创建额外的 JavaScript 线程
旁注:使用 JSON 深拷贝对象不是最佳实践。它是有损的(任何不可枚举的 属性 都被丢弃,任何值为 undefined
或函数的 属性 都被丢弃,任何用 Symbol 命名的 属性 都被丢弃,任何继承的属性 被删除,并且不维护原型),对于任何带有循环引用的东西都会失败,并且它会在文本中进行不必要的往返。有关在 JavaScript 中执行深度复制的各种方法,请参阅 this question's answers。
我正在制作一个 node.js 网络服务器来处理大负载、计算和副本,例如我需要一个大对象的深层副本:
const largeObject = { bla: "bla" } // ...
class Example {
constructor() {
this.copy = JSON.loads(JSON.stringify(largeObject))
this.copy.bla = "blo" // in reality the changes will different per request
}
doStuff(args) {
// do stuff with the deep copy
}
}
现在这工作正常,对于每个请求上下文,我都可以创建 1 个新的深层副本并在 class 中使用它。但是我的 class 变得越来越大而且没有结构,所以我想把它们分成不同的 classes。我想过用静态深拷贝实现一个基础 class,这样每个请求我都可以更改基础 class 上的副本并在我的另一个 class 中实现 class =]es.
const largeObject = { bla: "bla" } // ...
class Example {
static copy;
constructor() {
Example.copy = JSON.loads(JSON.stringify(largeObject))
Example.copy.bla = "blo" // in reality the changes will different per request
}
}
class DoWork {
constructor(someValue) {
this.someValue = someValue
}
doStuff(args) {
// do stuff Example.copy
}
}
出于性能原因,我只想在每个请求中深度复制对象一次,没有理由在每次 class 初始化时都深度复制对象。但是我害怕使用技术上比请求上下文更长寿的“全局”变量,我会遇到竞争条件和重叠上下文的问题。这是一个真正的问题还是 node.js 的单线程环境是否足够安全来处理这个问题。
该代码不会 运行 进入线程问题,不会。
Node.js 不是单线程的,但除非您通过 workers module, your code runs on a single thread, and even if you do create worker threads those threads run isolated from one another and from the main thread (they don't share a global environment; but they can communicate via messsaging and share memory in a very specific, bounded way via SharedArrayBuffer
).
旁注:使用 JSON 深拷贝对象不是最佳实践。它是有损的(任何不可枚举的 属性 都被丢弃,任何值为 undefined
或函数的 属性 都被丢弃,任何用 Symbol 命名的 属性 都被丢弃,任何继承的属性 被删除,并且不维护原型),对于任何带有循环引用的东西都会失败,并且它会在文本中进行不必要的往返。有关在 JavaScript 中执行深度复制的各种方法,请参阅 this question's answers。