admin管理员组文章数量:1134232
Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = url
as opposed to location.assign(url)
?
I guess I'm wondering if it takes more memory to access the method as opposed to setting the property.
Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = url
as opposed to location.assign(url)
?
I guess I'm wondering if it takes more memory to access the method as opposed to setting the property.
Share Improve this question edited Jan 13, 2016 at 20:41 Michał Perłakowski 92.4k30 gold badges163 silver badges185 bronze badges asked Apr 24, 2012 at 17:25 Doug WeaverDoug Weaver 1,7012 gold badges10 silver badges8 bronze badges 6 | Show 1 more comment7 Answers
Reset to default 62I 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.
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.
So, using jest:
window.location.assign = jest.fn();
myUrlUpdateFunction();
expect(window.location.assign).toBeCalledWith('http://my.url');
// Clean up :)
window.location.assign.mockRestore();
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, according to my tests. [Dead link to tests removed.]
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.
Is there any particular advantage/disadvantage in JavaScript memory consumption between using location.href = url as opposed to location.assign(url)?
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.
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.
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.
actually there is a difference i think
- location.href is a property which mean it is faster than calling a function but most importantly href property make user able to go back when clicking on back button on the browser
- location.replace() the user will not be able to go back to the current page.
- location.assign() the user will able to go back through back button like href but location.assign is better in terms of testing and mocking
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
location.assign was 15% slower than location.href.
I'd like to add a difference that I experienced using both while working in React which the above answers missing.
Analyze the following snippet in React:
return (<>location.href = "www://example.com"</>)
Vs
return (<>location.assign("www://example.com")</>)
In the fonmer case you'd actually see the string www://example.com getting typed on the DOM for a split second since it renders the text before this redirection happens.
To avoid that We need to use the latter location.assign()
本文标签: javascriptlocationhref property vs locationassign() methodStack Overflow
版权声明:本文标题:javascript - location.href property vs. location.assign() method - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736842326a1955154.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
location.assign
andlocation.href = ''
– Ruan Mendes Commented Apr 24, 2012 at 18:42location.href
vslocation.assign()
doesn't impact performance unless your app is changing location hundreds of times a second; and 2. if your app is doing that, that is the real problem you need to fix. – Jordan Gray Commented Jan 28, 2019 at 12:35