You should be caching the results of $media.attr() in advance of the test so you don't call it twice. Same for width, id and others.