Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
/**
* Strip extraneous information ("px") from a given value in preparation
* for getPositionProperties().
*/
Drupal.edit.util.stripPX = function(value) {
if (value) {
var index = value.indexOf('px');
if (index === -1) {
return NaN;
}
else {
return Number(value.substring(0, index));
}
}
else {
return NaN;
}
};
Feels a lot like you're trying to do what the native parseFloat does.
Also, it's generally advised to not use primitive constructors like Number() as they cause unnecessary overhead and can sometimes lead to getting a different type than you expected.
Attached patch removes the stripPX method and replaces all occurrences with parseFloat.
Comment | File | Size | Author |
---|---|---|---|
edit-parsefloat.patch | 2.25 KB | seutje |
Comments
Comment #1
seutje CreditAttribution: seutje commentedArgumentation: http://jsperf.com/constructor-vs-parse
Note how parseFloat is slightly slower in chrome (8%), but the stripPX function is far slower in FF (64%) and IE (54%)
General argumentation for constructors vs literal notation: http://jsperf.com/new-array
Comment #2
Wim LeersI didn't write this particular piece of code (@prestonso) did.
That being said, this looks like an excellent improvement — thanks! :) Will commit ASAP.
Comment #3
Wim LeersComment #4
Wim LeersCommitted just in time for the DrupalCon demo! :)
Thanks, Steve :)
http://drupalcode.org/project/edit.git/commit/ffb3ce3