September 3rd, 2009 / No Comments » / by Alex Shepko
Today I needed a function that pads a string to a certain length with another string. In PHP this function is called str_pad. Unfortunately JavaScript doesn’t have such function and I decided to write it myself.
Much to my surpise that was interesting becuase in so short and simple function it is possible to make a lot of performance mistakes.
The requirements for this function is the same as for PHP’s function, so it is an analog.
So, how was it ?
When I developed my first version of this function I decided to test it for performance. I’ve developed a small test script that executes this function 3000 times with different parameters. For all tests I used Firefox with Firebug’s Profiler. First test was taking about 1.1 seconds.
I decided to look around for similar solutions. I looked at PHP’s sources and founded there few good ideas. Then I’ve found a very nice site with already ported analog and I tried to test it too
Wow the result was much better than mine. This task was taking about 600ms.
Eventually I merged 3 ideas and I’ve added some tricks about string’s concatenation. I run a test again and now it takes only 147ms.
This function was improved significantly comparing with 1st time. Almost in 10 times.
Here is the source code:
String.PAD_LEFT = 0;
String.PAD_RIGHT = 1;
String.PAD_BOTH = 2;
String.prototype.str_pad = function(length, string, type)
{
if (undefined == type)
{
type = String.PAD_RIGHT;
}
else if (type < String.PAD_LEFT || type > String.PAD_BOTH)
{
return false;
}
if (undefined == string)
{
string = " ";
}
if (length <= 0 || (this.length - length) >= 0)
{
return false;
}
if ("string" != typeof(string))
{
string = new String(string);
}
var num_pad_chars = length - this.length,
pad_left = 0,
pad_right = 0;
switch(type)
{
case String.PAD_LEFT:
pad_left = num_pad_chars;
pad_right = 0;
break;
case String.PAD_RIGHT:
pad_left = 0;
pad_right = num_pad_chars;
break;
case String.PAD_BOTH:
pad_left = Math.floor(num_pad_chars / 2);
pad_right = num_pad_chars - pad_left;
break;
}
var left_mu = Math.ceil(pad_left/string.length);
var left = [];
while(left_mu)
{
left.push(string);
left_mu--;
}
left = left.join("").substr(0, pad_left);
var right_mu = Math.ceil(pad_right/string.length);
var right = [];
while(right_mu)
{
right.push(string);
right_mu--;
}
right = right.join("").substr(0, pad_right);
return left + this + right;
}
and some tests:
var a = "123456";
var b = "0";
print(a.str_pad(10, b));
print(a.str_pad(10, b, String.PAD_LEFT));
print(a.str_pad(10, b, String.PAD_RIGHT));
print(a.str_pad(10, b, String.PAD_BOTH));
print("---------------------");
print(a.str_pad(9, b));
print(a.str_pad(9, b, String.PAD_LEFT));
print(a.str_pad(9, b, String.PAD_RIGHT));
print(a.str_pad(9, b, String.PAD_BOTH));
print("--------------------");
var b = "0A";
print(a.str_pad(9, b));
print(a.str_pad(9, b, String.PAD_LEFT));
print(a.str_pad(9, b, String.PAD_RIGHT));
print(a.str_pad(9, b, String.PAD_BOTH));
print("----------------------");
print(a.str_pad(9, b, 10));
print("----------------------");
var a = "3";
var b = 0x18;
print(a.str_pad(9, b));
print("----------------------");
var a = 3; //error, unknown method
var b = 0x18;
print(a.str_pad(9, b));
print(a.str_pad(9, b, String.PAD_LEFT));
print(a.str_pad(9, b, String.PAD_RIGHT));
print(a.str_pad(9, b, String.PAD_BOTH));
Posted in: JavaScript, php
Tags: JavaScript, php, strings padding
June 18th, 2009 / No Comments » / by Alex Shepko
Today I’ve found one more trick about JavaScript and I think it will be interesting for someone else.
So, how do you usually get timestamp value with JavaScript?
I used the following code:
var timestamp = Math.round(new Date().getTime()/1000);
As you might know Firefox has its own function to get timestamp named now, so you can do static call like this:
var timestamp = Math.round(Date.now()/1000);
In this case you aren’t creating a new Date object. That is a good point.
But, what can we do for the rest browsers? Of course we can use known construction (see above). However there is one more possible construction:
var timestamp = Math.round(+new Date/1000);
As we see this construction is shorter. How does it work?
When script calls +new Date the script creates new Date object and makes a call to valueOf method of Date object.
In turn the valueOf method returns the number of milliseconds since midnight 01 January, 1970 UTC, so it is an equivalent to the getTime method.
I’ve check it with performance and they work identical.
Here is updated variant:
function timestamp()
{
return Math.round((Date.now &&Date.now() || +new Date)/1000);
}
Enjoy it!
Posted in: JavaScript
Tags: Date, JavaScript, timestamp, tricks
June 11th, 2009 / No Comments » / by Alex Shepko
You might know that PHP TestFest 2009 is still continuing.
I want to participate in this event as it is one of my favorite programming languages. I tried to find user group from Ukraine but I didn’t. It looks like nobody from Ukraine wants to improve it and its a pity.

Actually you don’t need to know C++ and it is enough to have your PHP knowledges. Of course you have to be an expert in PHP.
If you are interested you can start reading this page which explains how to write tests.
As a result if your test cases will be approved by mentor your tests will be committed to PHP CVS and you might get official PHP.net accounts with direct commit access to cvs.php.net and an @php.net email address. Yep! That’s not all – you will get elePHPant as a small present
Please contact me if you are interested and we can make a UA user group!
Posted in: php
Tags: elePHPant, php, test case, TestFest
June 8th, 2009 / No Comments » / by Alex Shepko
Today I’ve seen a new movie called HOME.
I am so affected by this movie and all people have to watch it. It explains in which situation the people live now and how we’ve been killing ourself for over 50 years.
Yes, for the last 50 years we’ve changed the Earth and we are about to destroy the nature, animals, everything that has been built for thousands years.
Here is a home page of that movie and here is a russian version.
This movie is going to be online till 14 June, so don’t postpone and watch it right now.
Posted in: the Earth
Tags: animal, Earth, nature, people
July 3rd, 2008 / No Comments » / by Alex Shepko
As you may know the company AOL has raised the protocol version from ICQ5 to ICQ6 and this caused connection problems for some ICQ clients. In this article I am explaining how to patch your pidgin by yourself.
- First of all we need to install the following packages: build-essential, fakeroot and devscripts
sudo apt-get install devscripts build-essential fakeroot
- Download the sources of libpurple0 because we need to patch this library
apt-get source libpurple0
- Download the patch and get patched the library
wget http://launchpadlibrarian.net/15741199/pidgin-2.4.2-icq.patch
tar xzf pidgin_2.4.1.orig.tar.gz
cd pidgin-2.4.1/
patch -p0 < ../pidgin-2.4.2-icq.patch
sudo apt-get build-dep libpurple0
debuild -us -uc
cd ..
sudo dpkg -i libpurple0_2.4.1-1ubuntu2_i386.deb
You can get an amd version instead of i386 depending of your platform
- Reload your pidgin and be happy
Posted in: linux, ubuntu
Tags: icq, pidgin, ubuntu
April 4th, 2007 / No Comments » / by Alex Shepko
Using SimpleXml extension to parse rdf file.
Here can be the following problem. For example we have next rdf file:
-
my desc
March, 10
and this php code to parse xml:
$rdf = simplexml_load_file($path2Rdf);
foreach ($rdf->item as $item)
{
echo $item->date;
}
However this code doesn’t work, because element “date” has a namespace prefix of “dc”, so the script doesn’t have an access to this namespace.
In order to have an access to date element we have to define the namespace in xml file
in php we have to use children method to get a collection of elements which belong to “dc” namespace:
$dc = $item->children('http://purl.org/dc/elements/1.1/');
echo $dc->date;
Posted in: php
March 29th, 2007 / No Comments » / by Alex Shepko
I found a good article about programmer levels. This article shows the points what a professional programmer should have.
Posted in: php
March 22nd, 2007 / No Comments » / by Alex Shepko
The other day i have read a fascinating novel about project management. I suppose any manager and developer should read this book independently of specialization and experience.
This book tells about people and their relations, phases of development and ordering, rules of staff recruitment, problems during developing and methods to solve them and a lot of other helpful advices.
Posted in: php
March 21st, 2007 / No Comments » / by Alex Shepko
Today i was writing following code:
interface iTest
{
function testMe($x);
}
class Test implements iTest
{
public function testMe($x, $y="World")
{
print $x . $y;
}
}
$test = new Test();
$test->testMe("Hello ");
The declaration of the method in class differs from interface, so I was surprised at this situation. Why php doesn’t generate any errors?!
Posted in: php