3

I wonder if anyone compared the two methods of handling ListItemCollection data in javascript:

var enumerator = collection.getEnumerator();
while(enumerator.moveNext()) {var current = enumerator.get_current(); /*...*/}
// vs.
var data = collection.get_data(), count = collection.get_count();
var index = 0, item = count > 0 ? data[index] : null, fieldValues = item ? item.get_fieldValues() : null;

Are there any drawbacks to the latter?

1

Below are parts of the code from sp.runtime.js:

SP.ClientObjectCollection = function SP_ClientObjectCollection(context, objectPath) {
    this.getEnumerator = this.$3M_1;
    SP.ClientObjectCollection.initializeBase(this, [context, objectPath]);
};
SP.ClientObjectCollection.prototype = {
//...
    $3M_1: function SP_ClientObjectCollection$$3M_1() {
        this.$2n_1(); /* checks that this.$2_1 is not null, otherwise throws CollectionNotInitialized exception */
        return new SP.ArrayListEnumerator(this.get_data());
    },
    get_count: function SP_ClientObjectCollection$get_count() {
        this.$2n_1();
        return (this.get_data()).length;
    },
    $2_1: null,
    get_data: function SP_ClientObjectCollection$get_data() {
        if (!this.$2_1) {
            this.$2_1 = [];
        }
        return this.$2_1;
    },
    fromJson: /* parses json response from executeQueryAsync and initializes this.$2_1 as an array of SP_ListItem objects */
//...
};

Take a look at $3M_1 function which is an alias for getEnumerator.
It just wraps array in a new object (ArrayListEnumerator):

SP.ArrayListEnumerator = function SP_ArrayListEnumerator($p0) {
    this.$2_0 = $p0;
    this.$M_0 = -1;
    this.$2K_0 = $p0.length;
    this.current = null;
};
SP.ArrayListEnumerator.prototype = {
    $2_0: null,
    $M_0: 0,
    $2K_0: 0,
    get_current: function SP_ArrayListEnumerator$get_current() {
        return this.$2_0[this.$M_0];
    },
    moveNext: function SP_ArrayListEnumerator$moveNext() {
        if (this.$2K_0 !== this.$2_0.length) {
            throw Error.invalidOperation(SP.ResResources.getString('CollectionModified'));
        }
        this.$M_0++;
        this.current = this.$2_0[this.$M_0];
        return this.$M_0 < this.$2_0.length;
    },
    reset: function SP_ArrayListEnumerator$reset() {
        this.$M_0 = -1;
        this.current = null;
    }
};

tl;dr:
I don't think there's any drawback to the latter.
On the contrary, it's faster and, imho, in most cases cleaner.

[p.s.] get_count can throw CollectionNotInitialized exception while get_data always returns you an array. Below is slightly faster and probably (arguably) safer:

var data = collection.get_data(), count = data.length;
  • 1
    Why am I not surprised to see a great answer from you Alex? :) – Alexey Krasheninnikov Feb 13 '18 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.