Some people commented in my AJAX Hello World with JPSpan article that the JPSpan example app was quite a bit slower. After a bit of testing it does seem that the performance problems comes from each JPSpan object having only 1 XMLHttpRequest object, this means if you use 1 object you can only have 1 outstanding call.
I would like to see this fixed with an pool of XMLHttpRequest objects at the JPSpan level, however im sure this will take some time so I have a solution for you today.
To use the pool you just need to include objectPool.js.
Then you just need to setup your pool. You’ll notice that the callback object recieves a reference to its parent remote object, this will be used to return the remote object back to the pool.
When you make a remote call now just get the get the object from the pool first.
Then in the callback when your in the callback method you return the object to the pool. Note that you just pass back a poolId that is set on the object not the entire Object.
Thats it your JPSpan apps are now a lot more responsive (of course this means your hitting the server a lot more).
Couple notes on the Object Pool.
Its pretty simplistic right now there are a couple things im going to update.
- Allow returnObject to take an object back just to make things more fool proof
- Add in the ability to limit the pool size and various options if that happens, either performing a function on current objects to recover them or just sleeping for a bit
- Adding some maintenance methods to let you get specific objects from the pool
Im also going to investigate a couple things but im not sure if its worth it.
- Allowing a pool to contain more then 1 type of object
- Adding in a method that checks if the object is ready to be returned to the pool instead of returning them with returnObject
If you think you’ll use this code or have any feedback on the items above let me know.