Drops a lot of unnecessary whitespace (but keeps newlines for legibility) and puts multiple defs into a single 'var' statement.
Reduces the head vars script from 1485 bytes to 1201 (nearly 20% savings), though this is of course reduced with gzipping (27 bytes, 4%). Cutting those bytes out of *every dang request* doesn't hurt though. :)
}
static function makeVariablesScript( $data ) {
- $r = array();
- foreach ( $data as $name => $value ) {
- $encValue = Xml::encodeJsVar( $value );
- $r[] = "var $name = $encValue;";
+ if( $data ) {
+ $r = array();
+ foreach ( $data as $name => $value ) {
+ $encValue = Xml::encodeJsVar( $value );
+ $r[] = "$name=$encValue";
+ }
+ $js = 'var ' . implode( ",\n", $r ) . ';';
+ return Html::inlineScript( "\n$js\n" );
+ } else {
+ return '';
}
- return Html::inlineScript( "\n\t\t" . implode( "\n\t\t", $r ) .
- "\n\t\t" );
}
/**