Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Small refactor of the URL replacement code #14

Merged
merged 2 commits into from

2 participants

@dcneiner

I ran some speed tests on the method currently used by Amplify to replace URL parameters with passed in data. I found using a regular expression replace with a callback was faster than redefining RegExp's for each item in a data structure.

You can see my perf tests here: http://jsperf.com/quick-templates-compared/2

These two commits handle the change to the faster method.

@scottgonzalez scottgonzalez merged commit 40ba7d5 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on May 30, 2011
  1. @dcneiner
  2. @dcneiner

    Small refactor of url replacement changes to bring up to the existing…

    dcneiner authored
    … code conventions. Updated bad ajax test that defined requests without a URL
This page is out of date. Refresh to see the latest.
Showing with 16 additions and 7 deletions.
  1. +14 −7 request/amplify.request.js
  2. +2 −0  request/test/unit.js
View
21 request/amplify.request.js
@@ -89,7 +89,8 @@ amplify.request.define = function( resourceId, type, settings ) {
(function( amplify, $, undefined ) {
-var xhrProps = [ "status", "statusText", "responseText", "responseXML", "readyState" ];
+var xhrProps = [ "status", "statusText", "responseText", "responseXML", "readyState" ],
+ rurlData = /\{([^\}]+)\}/g;
amplify.request.types.ajax = function( defnSettings ) {
defnSettings = $.extend({
@@ -97,11 +98,12 @@ amplify.request.types.ajax = function( defnSettings ) {
}, defnSettings );
return function( settings, request ) {
- var regex, xhr,
+ var xhr,
url = defnSettings.url,
data = settings.data,
abort = request.abort,
ajaxSettings = {},
+ mappedKeys = [],
aborted = false,
ampXHR = {
readyState: 0,
@@ -135,13 +137,18 @@ amplify.request.types.ajax = function( defnSettings ) {
if ( typeof data !== "string" ) {
data = $.extend( true, {}, defnSettings.data, data );
- $.each( data, function( key, value ) {
- regex = new RegExp( "{" + key + "}", "g");
- if ( regex.test( url ) ) {
- url = url.replace( regex, value );
- delete data[ key ];
+
+ url = url.replace( rurlData, function ( m, key ) {
+ if ( key in data ) {
+ mappedKeys.push( key );
+ return data[ key ];
}
});
+
+ // We delete the keys later so duplicates are still replaced
+ $.each( mappedKeys, function ( i, key ) {
+ delete data[ key ];
+ });
}
$.extend( ajaxSettings, defnSettings, {
View
2  request/test/unit.js
@@ -557,6 +557,7 @@ test( "data merging", function() {
}, "default data passed through" );
};
amplify.request.define( "test", "ajax", {
+ url: "",
data: {
foo: "bar",
bar: "baz",
@@ -577,6 +578,7 @@ test( "data merging", function() {
}, "data merged" );
};
amplify.request.define( "test", "ajax", {
+ url: "",
data: {
foo: "bar",
bar: "baz",
Something went wrong with that request. Please try again.