Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using the FitVids.js script to make YouTube video embeds work with a responsive layout. The script is < 3kb, but only about half of my pages actually have YouTube embeds.

In terms of total load-time for a page that doesn't contain a YouTube video, is it more costly to do a strpos() on the content or to include the script regardless?

I imagine this has a lot to do with the end-user's connection speed vs. the server's resources, and honestly, it's a low-traffic site with undemanding users so it really doesn't matter... I'm mostly asking because I don't have a good grasp of performance optimization and I want to learn.

share|improve this question
    
This is not really a WordPress-specific question … try a different site listed in the FAQ –  bungeshea Dec 20 '12 at 23:53
    
To be clear, I'm specifically wondering about any performance hit incurred by accessing $post->post_content on every page load. You could simplify the question down to: how costly is it to read and compare that value as part of a wp_enqueue_scripts action? As mentioned, I'm not clear on the performance impact, so even a "it's a trivial amount of processing impact" or "you'd want to think twice" would be helpful. –  Sarah Lewis Dec 21 '12 at 7:30
add comment

1 Answer

up vote 1 down vote accepted

What happens on the user’s side is off topic as @bungeshea said already. Once the user has loaded the file it will stay in the browser cache for a while, so you don’t have to worry about double loaded files anyway.

But let’s look at the server side and how WordPress handles this.

If you want to load a script on every page you just hook into wp_enqueue_scripts:

add_action( 'wp_enqueue_scripts', 'load_fitvid' );

function load_fitvid()
{
    wp_register_script( 'fitvid', plugins_url('/fitvid.js', __FILE__) );
    wp_enqueue_script( 'fitvid' );
}

Dead simple.

If you want to load the script only if there is a YouTube video, you have to hook into the_content, search for URLs with a host name youtube.com or youtu.be, check if WP_Embed::autoembed is activated (this converts URLs into embeds) and load the script if all these conditionals evaluate to TRUE.

This is slower. It is not very safe too: the default handler for videos might be overridden and run later than your content parser.

So: yes, there is a difference. I would use the simple method in your situation. If just 1% of all posts had a video … well this would be a different situation. I would hook into save_post then and update a custom meta value load_fitvid. On the front-end I’d test that value (much faster) and load the script then. But this is rather difficult to implement with existing posts.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.