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

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.


Solution 1:

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.

Solution 2:

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.

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();

Solution 3:

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.