Javascript location.href 属性与 location.assign() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10302905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:46:00  来源:igfitidea点击:

location.href property vs. location.assign() method

javascript

提问by Doug Weaver

Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = urlas opposed to location.assign(url)?

与使用location.href = url相比,在 JavaScript 内存消耗方面有什么特别的优势/劣势location.assign(url)吗?

I guess I'm wondering if it takes more memory to access the method as opposed to setting the property.

我想我想知道访问该方法是否需要更多内存而不是设置属性。

采纳答案by Tobias Krogh

I always used and never had problems with:

我一直使用并且从未遇到过以下问题:

location.href = url;

Calling a function should be slightly slower than accessing the property, but in terms of memory there should not be a big difference in my humble opinion.

调用函数应该比访问属性稍慢,但在内存方面,我的拙见应该没有太大的区别。

回答by Pere

I personally prefer calling the function instead, because calling a function gives me a better impression that something is running and that is not only a value of a variable that is changing.

我个人更喜欢调用函数,因为调用函数给我一个更好的印象,即某些东西正在运行,而不仅仅是一个正在改变的变量的值。

But probably yes, it may be true that location.href = url;is faster than location.assign(url), although it may depend on the JavaScript engine implementation, see the test I've just created.

但可能是的,它可能确实location.href = url;比 快location.assign(url),尽管它可能取决于 JavaScript 引擎实现,请参阅我刚刚创建的测试

回答by kieranroneill

I know this is old, but I stumbled on this when I was looking for a way to check my unit tests were redirecting to the correct url.

我知道这很旧,但是当我正在寻找一种方法来检查我的单元测试是否重定向到正确的 url 时,我偶然发现了这一点。

I would go with window.location.assign()if you are more concerned with testing. Using a function allows you to mock said function and check the url input parameters.

window.location.assign()如果你更关心测试,我会去。使用函数允许您模拟所述函数并检查 url 输入参数。

So, using jest:

所以,使用开玩笑:

window.location.assign = jest.fn();

myUrlUpdateFunction();

expect(window.location.assign).toBeCalledWith('http://my.url');

// Clean up :)
window.location.assign.mockRestore();

回答by Stijn de Witt

Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = url as opposed to location.assign(url)?

使用 location.href = url 与 location.assign(url) 相比,在 JavaScript 内存消耗方面有什么特别的优势/劣势吗?

NO

There is exactly zero difference.

完全零差异。

The reason for this is simple. Every time your browser loads a new page, it starts a fresh new Javascript 'VM' with the scripts for that page running in that VM. When running either of the statements in your question, you are instructing the browser to load a new page, which means destroying the current VM (and freeing up any memory associated with it) and loading a completely new VM for the new page.

这样做的原因是简单的。每次您的浏览器加载一个新页面时,它都会启动一个全新的 Javascript 'VM',该页面的脚本在该 VM 中运行。在运行问题中的任何一个语句时,您都在指示浏览器加载一个新页面,这意味着销毁当前 VM(并释放与之相关的任何内存)并为新页面加载一个全新的 VM。

Save for any weird browser bugs the net effect is always the same. Your scripts are running in a brand new VM with the exact same memory consumption.

除了任何奇怪的浏览器错误外,净效果始终相同。您的脚本在全新的 VM 中运行,内存消耗完全相同。

ulocation

定位

If you are working with the location object in the browser and you want to be able to run this code on Node JS (e.g. for testing or for isometric code), you can use ulocation, a universal/isometric implementation of the Location object. Full Disclosure: I am the author of that package.

如果您在浏览器中使用位置对象,并且希望能够在 Node JS 上运行此代码(例如用于测试或等距代码),您可以使用ulocation,位置对象的通用/等距实现。完全披露:我是该软件包的作者。

回答by Musoderelict

Tested my machine/browser, http://jsperf.com/location-href-vs-location-assign/2, for Chrome 40.0.2214.93 32-bit on Windows Server 2008 R2 / 7 64-bit

在 Windows Server 2008 R2 / 7 64 位上针对 Chrome 40.0.2214.93 32 位测试了我的机器/浏览器http://jsperf.com/location-href-vs-location-assign/2

location.assign was 15% slower than location.href.

location.assign 比 location.href 慢 15%。