Nick Lockwood 9f48c004ba Added setChildren() function
Summary:
public
Most of the time - especially during app startup - when we call UIManager.manageChildren(), we are actually just adding the first set of children to a newly created view.

This case is already optimized for in the JS code, by memoizing index arrays at various sizes, but this is not especially efficient since it is still sending an array of indices with each call that could be easily inferred on the native side instead.

I've added a hybrid native/JS optimization that improves the performance for this case. It's not a huge win in terms of time saved, but benchmarks show improvements in the ~1% range for several of the app startup metrics.

Reviewed By: tadeuzagallo

Differential Revision: D2757388

fb-gh-sync-id: 74f0cdbba93af2c04d69b192a8c2cc5cf429fa09
2015-12-15 06:56:31 -08:00

46 lines
1.2 KiB
JavaScript

/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule UIManager
* @flow
*/
'use strict';
var UIManager = require('NativeModules').UIManager;
if (!UIManager.setChildren) {
/**
* Index cache (used by setChildren())
*/
UIManager._cachedIndexArray = function(size) {
var cachedResult = this._cachedIndexArray._cache[size];
if (!cachedResult) {
var arr = [];
for (var i = 0; i < size; i++) {
arr[i] = i;
}
this._cachedIndexArray._cache[size] = arr;
return arr;
} else {
return cachedResult;
}
};
UIManager._cachedIndexArray._cache = {};
/**
* Fallback setChildren() implementation for Android
*/
UIManager.setChildren = function(containerTag, createdTags) {
var indexes = this._cachedIndexArray(createdTags.length);
UIManager.manageChildren(containerTag, null, null, createdTags, indexes, null);
}
}
module.exports = UIManager;