gpt4 book ai didi

javascript - 如果您使 javascript 中的已用对象无效,您是否正在保存可能的愚蠢垃圾收集?

转载 作者:行者123 更新时间:2023-11-30 05:45:39 24 4
gpt4 key购买 nike

如果您在 javascript 中使用过的对象无效,您是在节省可能的愚蠢垃圾回收吗?

例如。

如果您在 javascript 中遍历某些用户:

var users = [
{ FirstName: "Chris", LastName: "Pearson" },
{ FirstName: "Kate", LastName: "Johnson" },
{ FirstName: "Josh", LastName: "Sutherland" },
{ FirstName: "John", LastName: "Ronald" },
{ FirstName: "Steve", LastName: "Pinkerton" }
];

// Do something with the data, perhaps put it in a table

users = null;

现在是否值得取消该列表?有没有人了解性能提升背后的科学原理,或者这是在浪费时间等?

有时很难向人们解释你为什么要以某种方式做某事,我知道它在很多情况下保存了我的培根,当然,C++,但在 javascript 中,是否有任何实验来找出它是否值得javascript?找到一个我可以信任的 sizeof 函数,这让我哭了......如果有人已经做过这种实验,我将非常感激看到你的成果!

最佳答案

视情况而定。例如这里是没用的:

function putUsersInTable(){
var users = [
{ FirstName: "Chris", LastName: "Pearson" },
{ FirstName: "Kate", LastName: "Johnson" },
{ FirstName: "Josh", LastName: "Sutherland" },
{ FirstName: "John", LastName: "Ronald" },
{ FirstName: "Steve", LastName: "Pinkerton" }
];

// Do something with the data

users = null; // Useless, the data could be GC'd anyways
}

这里它将允许数据在其他情况下不能被 GC 处理:

function putUsersInTable(){
var users = [
{ FirstName: "Chris", LastName: "Pearson" },
{ FirstName: "Kate", LastName: "Johnson" },
{ FirstName: "Josh", LastName: "Sutherland" },
{ FirstName: "John", LastName: "Ronald" },
{ FirstName: "Steve", LastName: "Pinkerton" }
];

// Do something with the data

// This could be an event hander in the DOM or any global variable
outsidethisscope = function(){
// This anonymous function has access to users,
// But doesn't necessarily make use of it
console.log('Hi');
}

users = null; // Removes the only reference to that array,
// allowing it to be GC'd
}

关于javascript - 如果您使 javascript 中的已用对象无效,您是否正在保存可能的愚蠢垃圾收集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18108532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com