Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Web and WebWorks Development

Reply
Highlighted
Developer
Posts: 36
Registered: ‎02-06-2011
My Device: bold 9900
My Carrier: Vodacom
Accepted Solution

Which of the below code is more efficient?

1)

var sound = function(){
   var sound = new Audio();
   sound.src="audio/distroy.wav";
return sound.play();
}

sound();

 

2)

var sound = function(){
   var sound = new Audio();
   sound.src="audio/distroy.wav";
   return sound;
}

sound.play();

 

 

Developer
Posts: 1,280
Registered: ‎03-03-2011
My Device: Playbook, Z10, Q10, Z30 with Files & Folders and Orbit of course
My Carrier: Vodafone

Re: Which of the below code is more efficient?

[ Edited ]

(1) is slightly more efficient.

 

In (1) you are returning the result of the audio.play() function, which is void (empty). If your objective is to have as little lag/latency as possible, use this (or this). You could even shorten it a bit:

 

var sound = function(){  new Audio("audio/distroy.wav").play(); }

sound(); 

 

In (2) you are returning the Audio object. JavaScript will need to manage that (reference counting, garbage collection etc). Also, the lag between calling the sound function and the sound actually playing may be a little longer as there is a function return inbetween.

 

In reality there will probably be little difference.

Files & Folders, the unified file & cloud manager for PlayBook and BB10 with SkyDrive, SugarSync, Box, Dropbox, Google Drive, Google Docs. Free 3-day trial! - Jon Webb - Innovatology - Utrecht, Netherlands
Retired
Posts: 1,561
Registered: ‎04-12-2010
My Device: BlackBerry Z10
My Carrier: Bell

Re: Which of the below code is more efficient?

Generally I would recommend preloading your Audio resources during your application startup, and then you simply call play when that resource is needed. This should minimize any latency caused by the creation of the audio object (especially for larger audio files.)

Erik Oros | @WaterlooErik | eoros@blackberry.com | Developer Issue Tracker

If a reply has answered your question, please click Accept as Solution to help other visitors in the future.