Performance Numbers for Nitro Javascript Engine in IOS 4.3

TLDR: In summary, the original post is entirely accurate. Javascript running in web apps in Mobile Safari are roughly 2x faster than running in fullscreen mode via saving a web page to the home screen. Additionally, javascript is marginally faster in a custom UIWebView running in fullscreen, but still much slower than Mobile Safari.

There was a post on Hacker News earlier today that demonstrated the new Nitro javascript engine for Mobile Safari only appears to be used when a web app is running in Mobile Safari, but not when that page has been saved as a home screen object and then runs in fullscreen mode.

The post is here: IOS 4.3 Nitro JS engine disabled for full screen apps and uiwebview and the Hacker News post is here: http://news.ycombinator.com/item?id=2317804

I’ve run some numbers using the page from the HN post. The test embeds v0.9.1 of the Sunspider javascript test, which can then be run from the device.

I ran the following tests using a GSM iPhone 4 and iPad v1 32gb w/ 3G, both upgraded to IOS 4.3.

My tests included 3 uninterrupted runs of Sunspider in each of the following situations:

  1. Mobile Safari, native browser
  2. Page saved as home screen icon, full screen
  3. Custom iPhone app using a UIWebView that directly loads the Sunspider test linked to originally

Numbers:

ipad v1 (browser):
page1: 8145.2
page2: 8149.6
page3: 8166.3
avg: 8153.7ms
iPad v1 (fullscreen):
page1: 8383.7
page2: 8374.0
page3: 8201.7
avg: 8319.8ms
iPhone 4 (browser):
page1: 3981.3
page2: 4110.1
page3: 4087.5
avg:4059.33333
iPhone 4 (fullscreen):
page1: 10383.4
page2: 10594.6
page3: 10572.7
avg: 10416.9
iPhone 4 (fullscreen, custom UIWebView app)
page1: 9400.0
page2: 9422.8
page3: 9431.4
avg: 9417.6666

iPad v1 (browser):

  • page1: 3320.3
  • page2: 3248.5
  • page3: 3241.8
  • AVG: 3270.2 ms

iPad v1 (fullscreen):

  • page1: 8383.8
  • page2: 8375.4
  • page3: 8397.2
  • AVG: 8385.4666 ms

iPad (fullscreen, custom UIWebView app)

  • page1: 7689.7
  • page2: 7656.7
  • page3: 7703.9
  • AVG: 7683.4333 ms

iPhone 4 (browser):

  • page1: 3981.3
  • page2: 4110.1
  • page3: 4087.5
  • AVG: 4059.33333 ms

iPhone 4 (fullscreen):

  • page1: 10383.4
  • page2: 10594.6
  • page3: 10572.7
  • AVG: 10416.9 ms

iPhone 4 (fullscreen, custom UIWebView app)

  • page1: 9400.0
  • page2: 9422.8
  • page3: 9431.4
  • AVG: 9417.6666 ms

Finally my original tests were incorrect for the iPad. I had started the upgrade from IOS 4.2 to 4.3 but it had not completed. So, the following numbers are recorded for the iPad v1 using 4.2. Basically, in Mobile Safari javascript is more than 2x as fast javascript run in fullscreen mode.

iPad v1 4.2 (browser):

  • page1: 8145.2
  • page2: 8149.6
  • page3: 8166.3
  • AVG: 8153.7ms

iPad v1 4.2 (fullscreen):

  • page1: 8383.7
  • page2: 8374.0
  • page3: 8201.7
  • AVG: 8319.8ms

Easy jQuery Tooltip

Pretty basic post here. At work the other day, I needed a quick tooltip and didn’t want to install an over-burdened jQuery plugin, so I whipped this up. The usage is pretty basic, in that it uses the the “alt” attribute for the tooltip text. You can change the “x/y” variables to control the position offset from the mouse. Customize the styling in your own stylesheet. A sample is provided below.


var tooltips = { //tooltip to show Alt text
init:function(selector){
x = 10;
y = 10;

jQuery(selector).hover(function(e){
var tip = jQuery(this).attr('alt');
jQuery("body").append("

"+ tip +"

");
jQuery("#btooltip").css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px").fadeIn(250);

}, function(e){ jQuery('#btooltip').remove(); });

jQuery(selector).mousemove(function(e){

jQuery(selector).css("top",(e.pageY - xOffset) + "px").css("left",(e.pageX + yOffset) + "px");

});

}
}

jQuery(document).ready(function(){
//init tooltips
tooltips.init(jquerySelectorHere); //use any kind of normal jQuery selector.
});

You also need some simple styling. The only thing required is for display to be none for the fade-in to work. Here’s a sample.


#btooltip{
display:none;
background-color:#cc0000;
border:2px solid #000;
padding:5px;
}

ReloadCSS Stylesheet Reloader – make development a bit easier

ReloadCSS Bookmarklet: Reload CSS

The ReloadCSS source: Source

A while back I ran into an annoying problem. I was working on a couple of projects that were Java/JSF/JSP based. We were using Eclipse with Tomcat to run the project. The annoying bit was that whenever I made a change to a stylesheet, it would take several seconds to a minute before Eclipse updated the working deployment. Despite refreshing the browser repeatedly, a simple change just wouldn’t refresh immediately.

Also, if the remote dev servers for the database were running slow, each page refresh would take a while depending on what the application was trying to do.

My continuing interest in jQuery as a primary javascript development platform led me to a quick temporary solution. I wanted to make a JS bookmarklet that would dynamically reload all of the stylesheets on the page. I’ve since updated it a bit to make it more robust.

I’ve set this up as a code demo. You can add the bookmarklet to your browser by dragging the link to your toolbar in Firefox, or adding it as a favorite in IE. Safari is a little trickier, but basically just add it as a bookmark and it should work.

When you are on a page you are working on, hit the Reload CSS bookmarklet. This will insert a “Reload CSS” button floating in the top-right of the browser window. Every time you click this it will reload all stylesheets loaded via <link> tags. Sorry, it doesn’t load stylesheets with @import right now.

Each <link> tag href attribute gets a counter incremented as #num. This tricks the browser into updating the CSS.

The bookmarklet checks for jQuery in the DOM, then loads in v1.3.1. If you already have jQuery loaded, it isn’t loaded again. The script itself is then loaded from from my code stash here. When I make updates, they’ll automatically be updated. Also, feel free to copy the code itself if you want to use it or host it locally.

This has been tested and verified working in Firefox 3 and Safari on Mac OS and Windows. This is where I do my primary development and was my only concern when developing this.

Feedback is really appreciated, and hopefully this will help someone out.

ReloadCSS Bookmarklet: Reload CSS

The ReloadCSS source: Source

Introducing Fathomer

Project Page: http://code.google.com/p/fathomer/

I started working on this late last week after brainstorming with a friend. We wanted a way to dynamically promote some one’s Twitter account to users who are most likely to already be Twitter followers themselves. Imagine you are working at a large company with lots of traffic (like us) and want to promote your company’s Twitter account to your users, but don’t want to have to explain to the 95% of non-Twitter users just what the hell you’re talking about. The idea is that you can grow your community much easier if users are already on Twitter. As more of your general audience join Twitter themselves, they are introduced to the promo. Fathomer tries to work along the principal of gradual discovery of features.

If you are a Twitter user, you are most likely seeing Fathomer on this site. Its the orange badge in the top-right of the page.

Fathomer is a highly customizable tool that detects if a visitor to your site is a Twitter user and shows a badge, or ad, with your message and a link to your Twitter account.

It works by detecting if a user has visited the account settings page on Twitter, which is a URL you can only access once you have been logged in to the service. The user does not have to enter any personal account information, nor do you. You can configure Fathomer in many ways:

  1. Custom message – A default is used if none is set.
  2. Specify your account name. This is the only required piece of information and is used for display purposes only.
  3. Custom CSS and badge html – A complete default badge comes installed. Its possible to customize the CSS and even make your own html for the badge.
  4. Change detection url – You can change the Twitter url that is being checked if you need to.
  5. Target badge placement – By default, the badge loads in the top-right corner of the browser. However this can be changed to whatever load in whatever html element you want.

I’m also thinking about some improvements to the detection methods that could increase the precision of the targeted promotion. If there’s enough interest in Fathomer to warrant improvements, they’ll get added in. I’m also interested in adding some conversion metrics, but that takes Fathomer from being a simple and easy to install tool to being a larger project. If it grows, so will its capabilities.

You can visit the project page at http://code.google.com/p/fathomer/ or you can install Fathomer directly by adding the following script to your page somewhere and configuring the username and/or message.

All feedback is requested and appreciated!

<script type="text/javascript" src="http://fathomer.googlecode.com/files/fathomer-latest-min.js"></script>
<script type="text/javascript">
// Required. Your account name.
fathomer.account = 'yourAccountName';

// Optional. The message that is displayed.
//fathomer.message = 'Sample override message.';

// Optional. Can specify a specific element to attach to. ID, class, or css selector
//fathomer.target = '';

// Optional. Turn off default css if you have your own styles.
//fathomer.loadcss = false;

// Optional. Can be changed to a different Twitter url to check the visited history state.
//fathomer.urlcheck = '';

// Optional. If a custom html structure is needed, it can be loaded using standard jquery methods in altbadge. Properties are accessed/set via fathomer.var, e.g. fathomer.message.
//fathomer.altbadge = function(){};

fathomer.init();

</script>

Twitter "protects" API user status call… but doesn't

John Resig alerting about the authentication change

UPDATE 2/24/09: Twitter has changed their API and this technique no longer works.

For the last week or so, there’s been a lot of commentary about how you could detect if a Twitter user was visiting your site based on the response of a public, non-authenticated API call. It was documented at Ajaxian.

John Resig was one of the first to notice earlier today that Twitter has placed the API call in question behind http authentication. Indeed, the link he provides to Venture Hacks issues a login alert when you visit the page.

However, this does absolutely nothing to prevent a 3rd party from still accessing this information. Twitter is likely to fix this soon, but here’s how to use it in the mean time.

Basically, the API url that is now issuing the authentication requirement was this:

http://twitter.com/statuses/user_timeline.json&count=1&hasTwitter&suppress_response_codes

By simply changing the query string slightly, you bypass authentication and retrieve the user’s status data again if they are logged in. This works without the “/?callback=” part, but this is needed to have have Twitter wrap the json object so that it can be used in the browser, ala jsonp.

http://twitter.com/statuses/user_timeline/?callback=usrobj

If you use jQuery, the simple bit of code that returns this is:

$.ajax({
dataType: 'jsonp',
data: '',
jsonp: 'callback',
url: 'http://twitter.com/statuses/user_timeline/',
success: function(jsondata) {
alert(jsondata.toSource());
},
});

To use this to determine if a visitor is logged into Twitter or not, use the methods described in the Ajaxian article and just change the link. Happy hacking!

Javascript – RGB to Hexadecimal Color Converter

While working on a side project today, I found the need for an RGB to hex color code converter in javascript. There are a number out there within a couple of easy Google searches, but I wasn’t happy with any of the ones that I found.

Every example I found other people had posted over the years involves using a string using 0-9, a-f and then using character matching and some math shifting to return a hex conversion. This irked me because javascript is perfectly capable of doing these conversions with native methods.

So what you will run into from time to time is that some browsers like Firefox and Safari return the css color value of an element as an RGB format, like RGB(204,0,0). Other browsers, namely Internet Explorer, return the hex code, like #cc0000.

What I wanted was a reusable method that will convert an RGB value to the equivalent hex number. It also had to be flexible enough to not be abused if I pass in a hex or non-RGB value. This helps it play nicely across different browsers. Its using native javascript “parseInt” and “toString” methods to do the converting. So enough chit-chat, here’s the damn code. =)

rgbhex = function(rgbval){
var s = rgbval.match(/rgbs*x28((?:25[0-5])|(?:2[0-4]d)|(?:[01]?d?d))s*,s*((?:25[0-5])|(?:2[0-4]d)|(?:[01]?d?d))s*,s*((?:25[0-5])|(?:2[0-4]d)|(?:[01]?d?d))s*x29/);

var d='',e;
if(s){ s=s.splice(1); }
	if(s && s.length==3){
		d='';
		for(i in s){
			e=parseInt(s[i],10).toString(16);
			e == "0" ? d+="00":d+=e;
		} return '#'+d;
	}else{ return rgbval; }
}

The Double-class/Double-ID CSS Hack for IE6 & IE7

I haven’t seen this CSS hack for Internet Explorer before. I encountered this by accident today while trying to work out an IE6 display error. My preliminary Google searches and few hours of research didn’t find this documented anywhere, but I highly doubt I’m the first one to encounter this. In any event, I’m tentatively calling this the “Double-class” IE hack. It lets you target IE6 specifically, or IE6 and IE7 both. I haven’t found a way to specifically target IE7 using this.

In a nutshell, if you want to target IE6 use two class periods:

..className{ /* styles here */ }

To target IE7 and IE6 both, double-declare the class name:

..className, .className{ /* styles here */ }

Or, an alternate way to do this and target both IE6 and IE7 is this:

., .className{ /* styles here */ }

This also works with IDs:

.#classID{ /* styles here */ } /* IE6 Only */

., #classID{ /* styles here */ } /* IE6 and IE7 */