Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why not use querySelector instead of queryFirst #65

Closed
mitogh opened this issue Feb 12, 2017 · 8 comments
Closed

Why not use querySelector instead of queryFirst #65

mitogh opened this issue Feb 12, 2017 · 8 comments

Comments

@mitogh
Copy link
Member

mitogh commented Feb 12, 2017

Looking into the code I found the usage of queryFirst but I believe querySelector already does this.

Returns the first Element within the document (from the docs)

Is there any reason to not use querySelector instead? I would like to create a PR with the changes if required.

Thank you.

@mitogh mitogh changed the title Why not use querySelecto instead of queryFirst Why not use querySelector instead of queryFirst Feb 12, 2017
@widoz
Copy link
Contributor

widoz commented Feb 12, 2017

Have a look at the code and you'll see that queryFirst is a wrapper for query that return all of the element found using querySelectorAll.

I think that it's correct to use the querySelectorAll since most of the time you should manipulate all of the same objects within the page.

function query( selector ) {
	return Array.from( document.querySelectorAll( selector ) );
}

function queryFirst( selector ) {
	return query( selector )[ 0 ];
}

@mitogh
Copy link
Member Author

mitogh commented Feb 12, 2017

Yes thanks after I take a looked to the code I created the issue.

Because:

function queryFirst( selector ) {
	return query( selector )[ 0 ];
}

Creates the same result as querySelector so I think if querySelector already does that I don't see much benefit of creating a wrapper function for that. Unless the fact that if the selector is not found queryFirst is going to return undefined and querySelector is going to return null.

And about:

I think that it's correct to use the querySelectorAll since most of the time you should manipulate all of the same objects within the page.

I agree but in the cases where queryFirst is used there is no benefit or advantage in usage of querySelectorAll since you are first hand are requesting for the first element only.

@sirbrillig
Copy link

I agree but in the cases where queryFirst is used there is no benefit or advantage in usage of querySelectorAll since you are first hand are requesting for the first element only.

I agree, unless the intent is to abstract using the DOM API directly. If that's the case, it would still be more efficient to change queryFirst to just be an alias for document.querySelector.

@mcsf
Copy link
Contributor

mcsf commented Feb 17, 2017

Hey, @mitogh, thanks for raising a good question. Indeed it would be rather pointless to keep queryFirst as is, but there is a certain advantage to offering a separate interface. For instance, in #59, the following change:
8dd5176

@mcsf
Copy link
Contributor

mcsf commented Feb 17, 2017

To be clear, most of the advantage is to keep a common interface between query and queryFirst. Implementation-wise, we could rewrite it to use querySelector directly.

@mitogh
Copy link
Member Author

mitogh commented Feb 17, 2017

I think that would be just change the code inside queryFirst I can send a PR with the changes if required.

@mcsf
Copy link
Contributor

mcsf commented Feb 17, 2017

It's up to you, really, you're welcome to start a PR. Just bear in mind that the approach throughout this project has generally been "prototype first, code correctness eventually" 😄 , which is why you'll find tons of oddities in the code. We've been delaying refactoring up until the point where it's actually limiting us to implement something else.

@jasmussen
Copy link
Contributor

Closing for now, we can reopen if/when it becomes an issue in the plugin phase.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants