views:

455

answers:

4

I'm currently using the following function which is based on an example from the web, it is called every second to display the current progress through a video stream.

Is there something I could do to make this more efficient?

function secondstominutes(secs){

var s;
if(secs > 60){
  var min = Math.floor(secs / 60);
  s = min < 10 ? "0" : "";
  s += min +":";
  secs = secs - min * 60;
} else {
  s = "00:";
}

if(secs < 10){
  s+= "0" + Math.floor(secs);
} else {
  s += Math.floor(secs);
}
return s;
}
+1  A: 

Yes it can be a little simpler, and uses less Math.floor, local variables etc. Here mine proposition:

function secondstominutes(secs)
{
   return (Math.floor(secs/60))+":"+secs%60;
}

This will give result like:

0:1 for 1 sec 0:10 for 10 sec 1:1 for 61 sec

etc.

If you want spaces etc, it could be done like this:

function formatZero(number)
{
   return (number>9) ? number : "0"+number;
}

function secondstominutes(secs)
{
   return formatZero((Math.floor(secs/60)))+":"+formatZero(secs%60);
}

And this one is for obbsesive One-line function ppls ;)

function secondstominutes(secs)
{
   return ((arguments[1]=(Math.floor(secs/60)))<10?"0":"")+arguments[1]+":"+((arguments[2]=secs%60)<10?"0":"") + arguments[2];
}
Wilq32
+2  A: 
function secondstominutes(secs)
{
   var mins = Math.floor(secs / 60);
   secs = secs % 60;

   return (mins < 10 ? "0" + mins : mins) 
          + ":"
          + (secs < 10 ? "0" + secs : secs);
}
bobwienholt
Bob this is much simpler and very clear, thanks!
BrightLoudNoise
A: 

bobwienholts version is compact, fast and clear and it's very similar to the way I would have implemented it. For fun I did some profiling of a few different solutions and I found this to be a little faster, however it is not as clear and do an assignment semi hidden in a statement:

function secondstominutes(secs)
{
    var m = (secs / 60) | 0;

   return (m < 10 ? "0" + m : m) 
          + ":"
          + ( ( secs %= 60 ) < 10 ? "0" + secs : secs);
}

Explanation:

var m = (secs / 60) | 0;

Divide secs by 60 and binary "OR" the result with 0. The binary "OR" converts the value to an integer and by or-ing with 0 the integer part of the result is returned without modification. This is faster than calling Math.floor() but less clear. Since it is converted to an integer you limit the length of any movie to 2147483647 minutes ≈ 35791394 hours ≈ 1491308 days ≈ 4085 years.

( secs %= 60 )

This is the short form of doing (secs = secs % 60). It divides the value of secs by 60 and assigns secs the reminder (example 61 % 60 = 1). By profiling I found that it was a little faster to put that computation inside the return statement instead of on a row of its own.

(m < 10 ? "0" + m : m)

This computes the first statement m < 10 and if it is true the second statement is executed "0" + m else the third: m. In English: If m is less than 10 then a zero is added at the beginning else the value is returned as is.

some
I really appreciate the detailed explanation, but I've gone with Bob's as it is clearer to someone inheriting this code.Thanks!
BrightLoudNoise
No problem, I expected that you would choose bobwienholt since it is fast and clear. I only did this for fun to see if there was some way that was faster. I'm happy that you liked the detailed explanation! Good luck!
some
A: 

Can i make its reverse? I have a MM:SS variable and want to convert it to Seconds