Christian Harms's picture

JavaScript Object property getter benchmarks - part 2

The object access benchmarks last weeks was not surprising but a factor 5 between access "obj.a" and "obj.getA()" is interesting. In this second part I check the access time for properties on objects, created with ECMAScript 3 getter/setter and with the ECMAScript 5 Object.defineProperty variant (works on IE8+, FF3.7+, newest Webkit and Chrome 5) - see more on ECMAScript 5 compatibility table. Oliver asked to add getter tests - and the results produce differences with factor 15-100.Read more

Christian Harms's picture

JavaScript Object Access Micro benchmarks

After getting the tip to use JSLitmus for JavaScript microbenchmarks I grabbed my old "How to access JavaScript Object" code and build some tests with JSLitmus. The following question was sometimes very religious and only benchmarks can solve the problem:

The question is simple: what is faster?

  1. obj = {a:1};
  2.  
  3. //variant 1
  4. result = obj.a;
  5.  
  6. //variant 2
  7. result = obj['a'];
  8.  
  9. //variant 3 - build an object with getter function:
  10. result = obj.getA();
Read more

Nico Heid's picture

Google Code Jam - Africa Qualification Round 2010

The Google Code Jam is an interesting option to work on some challenging problems.
With the first qualification round coming up at May 7th, it's time to look at the Africa qualification round, to see what to expect.

You have 24 hours to solve three rather simple problems. Solving two problems brings you into the next round.

I will present my Java solutions. The Code Jam site provides the code of every contestant.

Store Credit

read problem description
You just need two nested loops to find the matching articles. Just a finger exercise.

  1. for (int i = 0; i < items.size(); i++) {
  2.                         for (int j = i + 1; j < items.size(); j++) {
  3.  
  4.                                 // is this the right shopping?
  5.                                 int newSum = items.get(i) + items.get(j);
  6.  
  7.                                 if (newSum == credit) {
  8.                                         return "Case #" + caseNr + ": " + (i + 1) + " " + (j + 1);
  9.                                 }
  10.                         }
  11.  
  12. }
Read more

Christian Harms's picture

IPv6 request crashed my google app engine application

My IP geo location script cache requests coming from the same ip range (subnet mask 255.255.255.0) named as class-c ip address (last number cutted). This value is the reference for look up in memcache. But yesterday I got an exception while calculating the ip from the request object.

  1. def get(self):
  2.     ipStr = self.request.remote_addr
  3.     classC = reduce(lambda a, b: int(a)*256+int(b), ipStr.split(".")[:3])
  4.     logging.info("request from classC ip address:: %d" % classC)
  5.     ...

This request crashed (TypeError: int argument required) by using an IPv6 address (found in the google app engine logs):Read more

Christian Harms's picture

Image resizing tips general and for python

Generating thumbnails from a collection of pictures is an easy task and there are many tools for the job. But I didn't find a tool who can clip out a centered box of my pictures to get quadratic thumbs of my originals. Because I am a developer and I dont want to try out many tools: I will hack a short python script (with help of the PIL) myself.

simple solution

The following python script can be find on many webpages. It will take all command line arguments as image filenames and convert it to 200x200 thumbnails with the default extension "_thumb.jpg" (build from the original filename):

  1. import Image, os, sys
  2.  
  3. for filename in sys.argv[1:]:
  4.     img = Image.open(filename).resize( (200,200) )
  5.     out = file(os.path.splitext(filename)[0]+"_thumb.jpg", "w")
  6.     try:
  7.         img.save(out, "JPEG")
  8.     finally:
  9.         out.close()
Read more

Matthias Reuter's picture

Better Javascript: Updates on the address book

Last night I met Jennifer again and apart from being hot it turns out she's pretty perceptive as well. When she read my previous article, she objected to some aspects of my address book.

Her point is, it's a waste of memory to store empty properties of an address. That's true, so I sat down and threw away those unneccessary bits:

var addresses = [{
    nickName  : "Mom",
    phoneNumber : "555-69-666"
},
{
    firstName : "Mathilda",
    lastName  : "Wozzle",
    phoneNumber : "555-343-37847"
},
{
    firstName : "Jennifer",
    phoneNumber : "555-467-4475"
}];

Unfortunately, we now encounter some problems: First checking for an empty string no longer is sufficent, and second, since undefined is not a string, concatenating is a problem:

var address = {
    nickName : "Mom",
Read more

Phillip Steffensen's picture

Developing a simple SOAP-webservice using Spring 3.0.1 and Apache CXF 2.2.6

In the past few years many techniques have been developed to help applications interact with each other. One of them are webservice-interfaces. These interfaces are extremly popular in the world of Java software development. One Framework that can be used to build such interfaces is Apache CXF. Apache CXF delivers a toolset to develop interfaces using different protocols like XML/HTTP, RESTful HTTP, Corba and SOAP. In this article i'd like to show how easy it could be to develop a simple SOAP-webservice based on Apache CXF 2.2.6 and the Spring Framework 3.0.1. You can download the full example at the bottom of this article.

Read more

Nico Heid's picture

Using an EntityManagerFactory with JPA, Hibernate and Wicket

If you read the previous article "@OneToMany Relationship with Java, Hibernate and Annotations" you might have noticed some imperfections in the persistence layer. On the code side we have been lazy by just using FetchType.EAGER. This of course puts extra stress on the database, because in our example, for each BlogPost all Comments are fetched, even though they might not be displayed.

Maybe you played around with the example version 0.1.1(download tarball). You might have noticed that if you switch to FetchType.LAZY you will most likely get a no session or session was closed on certain operations.
Read more
Matthias Reuter's picture

Better Javascript: Three women and one address book

This is about three women and a javascript address book. The first woman is my mom. Though she does have a first name and a last name, I have been referring to her as mom for my whole life, so her entry in my address book should be labelled "Mom".

The second woman is my probation officer, a Mrs. Mathilda Wozzle. I do not have a nickname for her, and obviously I don't want anybody who happens to look over my shoulder to see what she is. Her address book entry should therefore be labelled innocently "Mathilda Wozzle".

The third woman is Jennifer. I met her in a pub last week, and I do not remember her last name. I'm not even sure she gave me a last name, can't remember much of that night. The note she handed me contained only two bits, Jennifer and a phone number. I definitely want her in my address book and until further contact she has to go as "Jennifer".

I'm afraid it's getting technical now.

My address book now looks like this:

 Read more

Christian Harms's picture

Number crunching with javascript - a tutorial

This article will describe how to build a more complex javascript application. I chose to build a number cruncher for finding sociable numbers. There are many idle browser out there and the new Worker-thread object (available in modern browsers) offers the complete power of all cpu cores. In the first part I will describe how to inherit javascript objects with the optimization steps and discuss the usage of a profiler. The second part will improve the performance with using Web Worker in threads for greater computing power and how to handle it.

introduction

Did you heard of perfect numbers? No? A number is perfect if the number is equal to the sum all proper positive divisors. The old greek mathematics found the following examples (or try out the demo):

The 6 has as positive divisors 1, 2 and 3 and the sum is 6.Read more

Syndicate content